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
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-final-vs-static-access-modifier
Java - Final vs Static Access Modifier - GeeksforGeeks
July 23, 2025 - First, final is a non-access modifier applicable only to a variable, a method, or a class. The following are different contexts where the final is used. The static keyword in Java is mainly used for memory management.
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.

Discussions

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
Style poll: private static final LOG, log or logger?
I like the static final myself, but to each his or her own. ... I find it noisy to have this loggers in every class, especially if they then are never used in half of the classes. Also I don't see any advantage to use these static loggers. More replies More replies ... Presumably you could create your own Logger which passes the logging to a logger for the calling class. What do ya'll do with your logger object (java... More on reddit.com
🌐 r/java
23
4
February 14, 2015
When would you use final vs. private?
Final means that the value of the variable cannot be changed after it has first been set (ie it is a constant). Private means that the variable can only be accessed in the class in which it was declared. The two are not exclusive options. You can have a private final variable, which is a constant that can only be accessed within the class. Most of the time I use private variables, because modifying variables from other classes leads to "spaghetti code" that is problematic in large projects. When I have a value that will not change, ie a constant, I usually use public static final. Final prevents the constant from being modified, so it is safe to share its value with other classes. Static is normally added because most constants apply to all instances of the class. If a constant varies for each instance of a class, but it never changes once set, all you have to do is set its initial value in the constructor and not code any changes to it after that. If you do code changes to its value, it is usually because you need to. I can't think of a single case where not including final on a non-static variable has caused a difficult to trace error. Thus final without static is not a very useful combination. More on reddit.com
🌐 r/javahelp
7
1
August 8, 2017
🌐
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.
🌐
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.
🌐
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 - This is useful for preventing accidental changes to important variables. The static keyword indicates that a variable belongs to the class itself rather than to any particular instance of the class.
🌐
Coderanch
coderanch.com › t › 404575 › java › static-final-final-static
static final (or) final static (Beginning Java forum at Coderanch)
August 24, 2006 - For what it's worth, I would use "static final" too. It seems to be the prevalent style that most people are used to seeing. I'm just saying it's not actually part of Sun's standard. ... Your'e right. I was thinking of the JLS. I have the second edition handy and in section 8.3.1 (Field Modifers) they give the production: FieldModifier: one of public protected private static final transient volatile Below this it is written: If two or more (distinct) field modifiers appear in a field declaration, it is customary, though not required, that they appear in the order consistent with that shown above in the production for FieldModifier.
Find elsewhere
🌐
Coderanch
coderanch.com › t › 476812 › java › private-static-final
private static final (Beginning Java forum at Coderanch)
December 30, 2009 - Maybe if the user wants to expose the value of the variable only via public accessor methods (as normally is done for JavaBeans). ... Vishal Chaudhry wrote:why would someone what a variable to be static, final and also make it private. I use private static final for magic values, aka constants, that I want to define one place, never have them change, and use them a few times in the class.
🌐
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 ...
🌐
Codemia
codemia.io › knowledge-hub › path › private_final_static_attribute_vs_private_final_attribute
private final static attribute vs private final attribute
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises
🌐
Testbook
testbook.com › home › key differences › difference between static and final variable in java - testbook.com
Difference between Static and Final Variable in Java - Testbook.com
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.
🌐
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.
🌐
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 - Overall, the final keyword in Java ... In Java, the static keyword is used to indicate that a member (variable or method) of a class belongs to the class itself, rather than to any specific instance of the class....
🌐
Sololearn
sololearn.com › en › Discuss › 61607 › what-is-the-difference-between-static-and-final
what is the difference between static and final? | Sololearn: Learn to code for FREE!
And when we use "static" key word ... They are completely different things. Static makes your variable belongs to class and you can reach this variable from every instance of that class....
🌐
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....
🌐
GeeksforGeeks
geeksforgeeks.org › java › final-vs-static-vs-abstract-non-access-modifier
Final vs Static vs Abstract Non-Access Modifier - Java
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 GFG { // 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 ob
🌐
Quora
quora.com › What-is-difference-between-static-and-final-variable-when-boths-values-cant-be-changed
What is difference between static and final variable when both's values can't be changed? - Quora
Answer (1 of 11): We got lost here...static and final variables are different and have different uses although they can be used together whenever needed. And yes as already pointed by others here: " static variables can change values " while " final variables cannot....they're constant ". Let's c...
🌐
Web Reference
webreference.com › java › oop › static-final
Java Static and Final Keywords Complete Guide
Learn Java static and final keywords including static variables, methods, blocks, final variables, methods, classes, and combining static final for constants.
🌐
Quora
quora.com › Whats-the-difference-between-a-public-static-final-and-a-public-final-in-Java
What's the difference between a public static final and a public final in Java? - Quora
Answer (1 of 3): public static final fields These fields are so-called global constants in many other languages. Once a program starts to run, the value of these fields can never change. Because they don’t change, Java compiler often inlines them into wherever they are referenced as long as thei...
🌐
Quora
quora.com › When-should-static-and-final-variable-used-in-java
When should static and final variable used in java? - Quora
Answer (1 of 5): static variable There will be times when you will want to define a class member Variable that will be used independently of any object of that class. Normally, a class member must be accessed only in conjunction with an object of its class. However, it is possible to create a me...