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.

Answer from Ashish on Stack Overflow
🌐
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.
🌐
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.
Discussions

java - Difference between Static and final? - Stack Overflow
I'm always confused between static and final keywords in java. How are they different ? More on stackoverflow.com
🌐 stackoverflow.com
Difference between static, final and static final (variable pov)
Final can only be set once, but it still can be set and it can have a different value for each instance. Say you have a car class. Your make and model could be final and set in the constructor. Each new car can have a different make and model, but you can't change their make and model after it is set. Static variables are specific to the class and are cannot have seperate values for each instance. Your car class can have a car registry. To track all cars ever created, there is a static int that starts at 0 and is incremented by one each time a car is constructed. If this wasn't static, each car would have its own totalCars which would be one, since each instance starts at zero and that gets incremented in the constructor. However, since it is static, it is initialized with the class and each new car increases it by one so that it's value is equal to how many cars have been made. Static final is for constants. You need to know how fast your cars accelerate due to gravity. This value is set to 9.8. It is static because it doesn't matter which instance, it belongs to the class as a whole and you don't allow the value to be changed, so it can be final. More on reddit.com
🌐 r/javahelp
12
9
March 3, 2022
Final, static, and static final in Java
If you make a variable or method static, it would only be accessed by the class itself and not by an instance of the class (object) This is already not true. A static variable can be accessed by instances of the class perfectly fine. I guess your further confusion results from this misunderstanding. A static class variable will be created once for the class, and not once for every instance. More on reddit.com
🌐 r/learnprogramming
3
1
September 28, 2021
Why are final variables used in java?
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
🌐 r/javahelp
26
15
January 6, 2022
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
Can a final variable be static in Java?
Yes, a variable can be both final and static in Java. This combination is used to define constants that are common to all instances of a class. For example, public static final int MAX_SIZE = 100; declares a constant MAX_SIZE that is shared across all instances of the class and cannot be changed.
🌐
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
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.

🌐
TutorialsPoint
tutorialspoint.com › difference-between-static-and-final-in-java
Difference Between Static and Final in Java
May 15, 2025 - 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.
🌐
BYJUS
byjus.com › gate › difference-between-static-and-final-variable-in-java
Difference between Static and Final Variable in Java
March 31, 2023 - On the other hand, the final keyword is used to proclaim a constant variable and to bind the user from accessing a method, variable, or class. Let’s figure out some major differences between static and final keywords in Java.
Find elsewhere
🌐
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 - This means that the variable cannot be modified and it is also shared by all instances of the class. A final static variable is usually declared in all uppercase letters. ... public class Constants { public static final int MAX_VALUE = 100; ...
🌐
DataCamp
datacamp.com › doc › java › static-and-final-keyword
Static and Final Keyword
In this example, counter is a static variable, and incrementCounter is a static method. Both can be accessed without creating an instance of StaticExample. The final keyword is used to declare constants, prevent method overriding, and restrict inheritance.
🌐
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...
🌐
DigitalOcean
digitalocean.com › community › tutorials › static-keyword-in-java
static keyword in java | DigitalOcean
August 4, 2022 - A static variable is a class variable ... the instances of Object, they are not thread safe. Usually, static variables are used with the final keyword for common resources or constants that can be used by all the objects....
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-final-vs-static-access-modifier
Java - Final vs Static Access Modifier - GeeksforGeeks
July 23, 2025 - The following are different contexts where the final is used. The static keyword in Java is mainly used for memory management. The static keyword in Java is used to share the same variable or method of a given class.
🌐
Reddit
reddit.com › r/javahelp › difference between static, final and static final (variable pov)
r/javahelp on Reddit: Difference between static, final and static final (variable pov)
March 3, 2022 -

final - The value assigned to the variable cannot be changed.

static - Even in different class instances, compiler will allocate the same memory location to the variable which it had assigned no matter how many instances of that class. The value can be changed.

My question is: Why is the final keyword not allocating the same memory location to the variable, if the value cannot change?

Let us have different instances of a class and still we will have the same value in the variable as it has been declared as final.

Why do we need to write static final then, it could have been done via final only, am I missing on thinking some cases?

If someone can please clarify?

Top answer
1 of 5
11
Final can only be set once, but it still can be set and it can have a different value for each instance. Say you have a car class. Your make and model could be final and set in the constructor. Each new car can have a different make and model, but you can't change their make and model after it is set. Static variables are specific to the class and are cannot have seperate values for each instance. Your car class can have a car registry. To track all cars ever created, there is a static int that starts at 0 and is incremented by one each time a car is constructed. If this wasn't static, each car would have its own totalCars which would be one, since each instance starts at zero and that gets incremented in the constructor. However, since it is static, it is initialized with the class and each new car increases it by one so that it's value is equal to how many cars have been made. Static final is for constants. You need to know how fast your cars accelerate due to gravity. This value is set to 9.8. It is static because it doesn't matter which instance, it belongs to the class as a whole and you don't allow the value to be changed, so it can be final.
2 of 5
5
You have two completely separate concepts here: final - the value cannot be changed, once it has been assigned. (Unstated assumption here is that the value can be assigned after creation, and that it can be declared on a per-instance basis.) static - all instances share a common value. Nothing less, nothing more. What that means is that if one instance changes the value, it changes for all instances. Why do we need to write static final then, You don't need to write that, unless you want a specific behavior. Namely, that the variable can only be set once, and that it will be shared by all instances. Typically, you would declare the value with the variable, but I don't think you have to do that. It would be possible for you to create multiple instances, and then have one of them set the value for all of them, permanently.
🌐
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.
🌐
Xenovation
xenovation.com › home › blog › java › java final or java static
Java final or Java static | XENOVATION
December 22, 2022 - ... As we already explained them ... the two of them: The static keyword is applicable to a nested static calss, variables, methods and blocks, while final keyword is applicable to class methods and variables....
🌐
Quora
quora.com › What-is-the-difference-between-static-and-final-variables-in-Java
What is the difference between static and final variables in Java? - Quora
Answer (1 of 4): Static keyword in Java is used for memory management mainly. We can apply java static keyword with variables, methods, blocks and nested class. The static keyword belongs to the class than an instance of the class.
🌐
Reddit
reddit.com › r/learnprogramming › final, static, and static final in java
r/learnprogramming on Reddit: Final, static, and static final in Java
September 28, 2021 -

Please correct me if I am wrong, and explain where my understanding is incomplete.

From what I've read both static and final are keywords. If you make a variable or method static, it would only be accessed by the class itself and not by an instance of the class (object). A static class would be useful if it was nested inside another class and you didn't want an instance of the outer class to access the inner class.

If you make a variable final, then it is initialized once and not changed. A final class can't be subclassed, and a final method can't be overwritten. If a variable is static final, then it cannot be changed and it can only be accessed by the class itself and not an object of the class.

People use static all the time, but why is it useful to limit the access of a method or variable to the class itself and not any instances of it?

🌐
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   5 days ago
🌐
W3Schools
w3schools.com › java › ref_keyword_final.asp
Java final Keyword
The final keyword is called a "modifier". You will learn more about these in the Java Modifiers Chapter. Read more about attributes our Java Class Attributes Tutorial. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: [email protected] · If you want to report an error, or if you want to make a suggestion, send us an e-mail: [email protected] · HTML Tutorial CSS Tutorial JavaScript Tutorial How To Tutorial SQL Tutorial Python Tutorial W3.CSS Tutorial Bootstrap Tutorial PHP Tutorial Java Tutorial C++ Tutorial jQuery Tutorial
🌐
GeeksforGeeks
geeksforgeeks.org › java › final-static-variable-java
Final Static Variable in Java - GeeksforGeeks
April 24, 2025 - If the static variable is declared as final, then we have to perform initialization explicitly, whether we are using it or not, and the JVM won’t provide any default value for the final static variable. Example: Initialization at the Time of Declaration ... // Java program to illustrate that final // static variable can be initialized // at the time of declaration class Test { final static int x = 10; public static void main(String[] args) { System.out.println(x); } }
🌐
GeeksforGeeks
geeksforgeeks.org › java › static-keyword-java
static Keyword in Java - GeeksforGeeks
Static methods and variables can’t access non-static members directly. Static methods can’t be overridden because they belong to the class, not instances. The static keyword can be applied to four main components:
Published   1 month ago