🌐
DataCamp
datacamp.com › doc › java › static-and-final-keyword
Static and Final Keyword
Final Classes: Use final classes to prevent inheritance when you want to provide a complete and unmodifiable implementation. Static Initialization: Use static blocks for complex static initialization tasks that cannot be handled by a simple assignment. ... Memory Considerations: Be cautious ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-final-vs-static-access-modifier
Java - Final vs Static Access Modifier - GeeksforGeeks
July 23, 2025 - In the case of instance variable for every object, a separate copy will be created but in the case of static variable, a single copy will be created at class level and shared by every object of that class. ... // Java Program to Illustrate Static Access Modifier // Importing required classes import java.io.*; import java.util.*; // Main class class Geeks { // Creating a static variable and // initializing a custom value static int x = 10; // Creating a instance variable and // initializing a custom value int y = 20; // Main driver method public static void main(String[] args) { // Creating an
People also ask

What are the best practices for using final and static in Java?
Best practices for using final: Use final for variables that should not change after initialization. Apply final to methods that should not be overridden to maintain the intended behavior across subclasses. Use final on classes that are not meant to be extended, often for reasons of security, simplicity, or design. Best practices for using static: Use static variables for class-level constants or fields shared by all instances. Apply static to methods that do not require access to instance-specific data. Utilize static blocks for initializing static variables or executing static initialization
🌐
shiksha.com
shiksha.com › home › it & software › it & software articles › programming articles › difference between final and static in java
Difference Between Final and Static in Java
What does the static keyword signify in Java?
The static keyword in Java is used to indicate that a particular field, method, or block belongs to the class, rather than instances of the class. This means that static members can be accessed without creating an instance of the class. Static variables are shared among all instances of a class, making them class variables. Static methods can be invoked without an object, and static blocks are used for static initializations.
🌐
shiksha.com
shiksha.com › home › it & software › it & software articles › programming articles › difference between final and static in java
Difference Between Final and Static in Java
How do final and static affect inheritance in Java?
Final and static keywords affect inheritance differently. A final class cannot be extended, meaning no subclass can inherit from a final class. A final method cannot be overridden by subclasses, which means the functionality defined by the final method is the same for all subclasses. On the other hand, static members (methods or variables) are not inherited in the same way instance members are; instead, they belong to the class level. While static methods can be hidden by subclasses (if they declare a static method with the same signature), this is not the same as overriding and does not follo
🌐
shiksha.com
shiksha.com › home › it & software › it & software articles › programming articles › difference between final and static in java
Difference Between Final and Static in Java
🌐
Medium
medium.com › @kumar.atul.2122 › java-static-and-final-keywords-1e246606b67b
Java: Static and final keywords. Final keyword: | by Atul Kumar | Medium
February 19, 2023 - public class Constants { public static final int MAX_VALUE = 100; public static final String APP_NAME = "My Application"; } Performance optimization: The final keyword can be used as a performance optimization technique.
🌐
W3Schools Blog
w3schools.blog › home › final keyword in java
final keyword in java - W3schools
August 27, 2014 - A variable declared with final keyword is known as final variable. It may be member variable or local variable. final variables are constants in java and they are generally declared with static keyword. As final variables are treated as constants they can’t reassign.
🌐
Grow As You Learn
letsunderstandjava.com › home › static, this, super & final keyword
Static, this, super & final keyword - Grow As You Learn
August 6, 2024 - Call to super() must be the first statement in a derived class constructor. Eg – Student() { super(); System.out.println(“constructor”); } To apply restrictions on user access, the final keyword is used.
🌐
Shiksha
shiksha.com › home › it & software › it & software articles › programming articles › difference between final and static in java
Difference Between Final and Static in Java
March 28, 2024 - In essence, final is used to declare entities that cannot be modified after they are set. ... The static keyword in Java is used to indicate that a particular field, method, or block belongs to the class, rather than instances of the class.
🌐
TutorialsPoint
tutorialspoint.com › difference-between-static-and-final-in-java
Difference Between Static and Final in Java
The final variable in Java is a variable whose value we can not change or re-initialize. We can declare the variable as final by just writing the final keyword before the variable.
🌐
W3Schools
w3schools.com › Java › ref_keyword_final.asp
Java final Keyword
Java Examples Java Compiler Java ... } } ... The final keyword is a non-access modifier used for classes, attributes and methods, which makes them non-changeable (impossible to inherit or override)....
Find elsewhere
🌐
W3Schools
w3schools.com › JAVA › java_variables_final.asp
Java Constants (final keyword)
Java Examples Java Compiler Java Exercises Java Quiz Java Server Java Syllabus Java Study Plan Java Certificate ... When you do not want a variable's value to change, use the final keyword.
🌐
BYJUS
byjus.com › gate › difference-between-static-and-final-variable-in-java
Difference between Static and Final Variable in Java
March 31, 2023 - These variables help in saving a lot of memory and getting the memory at the time of loading the class. When a variable is expressed with the final keyword, then it is not possible to change its value.
Top answer
1 of 11
279

The static keyword can be used in 4 scenarios

  • static variables
  • static methods
  • static blocks of code
  • static nested class

Let's look at static variables and static methods first.

Static variable

  • It is a variable which belongs to the class and not to object (instance).
  • Static variables are initialized only once, at the start of the execution. These variables will be initialized first, before the initialization of any instance variables.
  • A single copy to be shared by all instances of the class.
  • A static variable can be accessed directly by the class name and doesn’t need any object.
  • Syntax: Class.variable

Static method

  • It is a method which belongs to the class and not to the object (instance).
  • A static method can access only static data. It can not access non-static data (instance variables) unless it has/creates an instance of the class.
  • A static method can call only other static methods and can not call a non-static method from it unless it has/creates an instance of the class.
  • A static method can be accessed directly by the class name and doesn’t need any object.
  • Syntax: Class.methodName()
  • A static method cannot refer to this or super keywords in anyway.

Static class

Java also has "static nested classes". A static nested class is just one which doesn't implicitly have a reference to an instance of the outer class.

Static nested classes can have instance methods and static methods.

There's no such thing as a top-level static class in Java.

Side note:

main method is static since it must be be accessible for an application to run before any instantiation takes place.

final keyword is used in several different contexts to define an entity which cannot later be changed.

  • A final class cannot be subclassed. This is done for reasons of security and efficiency. Accordingly, many of the Java standard library classes are final, for example java.lang.System and java.lang.String. All methods in a final class are implicitly final.

  • A final method can't be overridden by subclasses. This is used to prevent unexpected behavior from a subclass altering a method that may be crucial to the function or consistency of the class.

  • A final variable can only be initialized once, either via an initializer or an assignment statement.
    It does not need to be initialized at the point of declaration, this is called a blank final variable, but in this case:

    • A blank final instance variable must be assigned at every constructor of its class.
    • A blank final static variable must be assigned in a static initializer in its class.

Note: If the variable is a reference, this means that the variable cannot be re-bound to reference another object. But the object that it references is still mutable, if it was originally mutable.

When an anonymous inner class is defined within the body of a method, all variables declared final in the scope of that method are accessible from within the inner class. Once it has been assigned, the value of the final variable cannot change.

2 of 11
45

static means it belongs to the class not an instance, this means that there is only one copy of that variable/method shared between all instances of a particular Class.

public class MyClass {
    public static int myVariable = 0; 
}

//Now in some other code creating two instances of MyClass
//and altering the variable will affect all instances

MyClass instance1 = new MyClass();
MyClass instance2 = new MyClass();

MyClass.myVariable = 5;  //This change is reflected in both instances

final is entirely unrelated, it is a way of defining a once only initialization. You can either initialize when defining the variable or within the constructor, nowhere else.

note A note on final methods and final classes, this is a way of explicitly stating that the method or class can not be overridden / extended respectively.

Extra Reading So on the topic of static, we were talking about the other uses it may have, it is sometimes used in static blocks. When using static variables it is sometimes necessary to set these variables up before using the class, but unfortunately you do not get a constructor. This is where the static keyword comes in.

public class MyClass {

    public static List<String> cars = new ArrayList<String>();

    static {
        cars.add("Ferrari");
        cars.add("Scoda");
    }

}

public class TestClass {

    public static void main(String args[]) {
        System.out.println(MyClass.cars.get(0));  //This will print Ferrari
    }
}

You must not get this confused with instance initializer blocks which are called before the constructor per instance.

🌐
Baeldung
baeldung.com › home › java › core java › difference between “final static” and “static final”
Difference Between “final static” and “static final” | Baeldung
January 5, 2024 - The static keyword indicates that a variable belongs to the class itself rather than to any particular instance of the class. This means that there is only one copy of the variable, which is shared by all instances of the class. When we combine the final and static keywords, we create a variable that is both constant and class-level...
🌐
W3Schools
w3schools.com › java › ref_keyword_static.asp
Java static Keyword
The static keyword is a non-access modifier used for methods and attributes. Static methods/attributes can be accessed without creating an object of a class. Read more about modifiers in our Java Modifiers Tutorial.
🌐
Xenovation
xenovation.com › home › blog › java › java final or java static
Java final or Java static | XENOVATION
December 22, 2022 - The method declared as final cannot ... as final, other classes cannot inherit that final class. ... The static keyword in Java is used for memory management....
🌐
Web Reference
webreference.com › java › oop › static-final
Java Static and Final Keywords Complete Guide
public class Constants { // Mathematical constants public static final double PI = 3.14159265358979323846; public static final double E = 2.71828182845904523536; public static final double GOLDEN_RATIO = 1.61803398874989484820; // Application constants public static final String APP_NAME = "MyApplication"; public static final String VERSION = "1.0.0"; public static final int MAX_USERS = 1000; // Configuration constants public static final int DEFAULT_TIMEOUT = 30000; // milliseconds public static final int MAX_RETRY_ATTEMPTS = 3; public static final String DEFAULT_CHARSET = "UTF-8"; // Collect
🌐
Sanfoundry
sanfoundry.com › static-final-keyword-in-java
Static & Final Keyword in Java - Sanfoundry
March 18, 2025 - The program then prints the current lesson topic using the static variable courseName, making it more structured and organized. The final keyword in Java is used to create constants, restrict method overriding, and prevent inheritance. It ensures that a variable, method, or class remains unchanged ...
🌐
IncludeHelp
includehelp.com › java › final-and-static-keywords-in-java-with-example.aspx
Final and static keywords in Java with Example - IncludeHelp
It is used to avoid method overriding in java. In other words if final keyword is assigned before any function then one cannot use the name of function to create another function.
🌐
TheServerSide
theserverside.com › video › Why-we-use-static-final-in-Java-for-constants
Why we use static final in Java for constants | TheServerSide
To create a global constant shared by every instance of a class, you combine Java's static and final keywords. The static keyword means the value is the same for every instance of the class.
🌐
GeeksforGeeks
geeksforgeeks.org › java › final-keyword-in-java
final Keyword in Java - GeeksforGeeks
Static final variables represent constants shared across all objects. Blank final variables are declared without initialization and must be assigned once before use. Local final variables inside methods must be initialized within their block.
Published   July 22, 2014