Static Nested Classes

As with class methods and variables, a static nested class is associated with its outer class. And like static class methods, a static nested class cannot refer directly to instance variables or methods defined in its enclosing class: it can use them only through an object reference. Note: A static nested class interacts with the instance members of its outer class (and other classes) just like any other top-level class. In effect, a static nested class is behaviorally a top-level class that has been nested in another top-level class for packaging convenience.

Static nested classes are accessed using the enclosing class name:

OuterClass.StaticNestedClass

For example, to create an object for the static nested class, use this syntax:

OuterClass.StaticNestedClass nestedObject =
     new OuterClass.StaticNestedClass();

http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html

Final Classes

A class that is declared final cannot be subclassed. This is particularly useful, for example, when creating an immutable class like the String class.

http://docs.oracle.com/javase/tutorial/java/IandI/final.html

Answer from Jabir on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › java › final-static-variable-java
Final Static Variable in Java - GeeksforGeeks
April 24, 2025 - For final static variable, it is compulsory that we should perform initialization before class loading completion. We can initialize a final static variable at the time of declaration.
🌐
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.
🌐
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?

🌐
TutorialsPoint
tutorialspoint.com › difference-between-static-and-final-in-java
Difference Between Static and Final in Java
In this article, we will learn ... in Java. Two of the most frequently used and frequently confused keywords are static and final. Although they may at times be used together in code, they have basically different functions. The static keyword can be applied to nested static classes, variables, ...
🌐
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.
🌐
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 ...
🌐
Baeldung
baeldung.com › home › java › core java › static final variables in java
Static Final Variables in Java | Baeldung
March 17, 2024 - Simply put, static final variables, also called constants, are key features in Java to create a class variable that won’t change after initialization.
Find elsewhere
🌐
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
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.

🌐
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.
🌐
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.
🌐
Coderanch
coderanch.com › t › 378156 › java › static-final-method
static final method (Java in General forum at Coderanch)
programming forums Java Mobile ... it mean? A final method means that you don't want child classes to override it. But if it's static, it cannot be overridden anyway....
🌐
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 be overridden by the subclass of that class in which final method is declared. And as for the classes, when they are declared as final, other classes cannot inherit that final class. ... The static keyword ...
🌐
Coderanch
coderanch.com › t › 404575 › java › static-final-final-static
static final (or) final static (Beginning Java forum at Coderanch)
The only mention of final is in the section on Naming Conventions, where the give some code examples of constants. And yes, the examples do say "static final" - but they're just examples. Not rules. The rule that the examples are illustrating there is just about the name of the constant - nothing about the order of the modifiers. For what it's worth, I would use "static final" too.
🌐
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.
🌐
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 ... 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....
Top answer
1 of 14
104

final indicates that the value of the variable won't change - in other words, a constant whose value can't be modified after it is declared.

Use public final static String when you want to create a String that:

  1. belongs to the class (static: no instance necessary to use it).
  2. won't change (final), for instance when you want to define a String constant that will be available to all instances of the class, and to other objects using the class.
  3. will be a publicly accessible part of the interface that the class shows the world.

Example:

public final static String MY_CONSTANT = "SomeValue";

// ... in some other code, possibly in another object, use the constant:
if (input.equals(MyClass.MY_CONSTANT))

Similarly:

public static final int ERROR_CODE = 127;

It isn't required to use final, but it keeps a constant from being changed inadvertently during program execution, and serves as an indicator that the variable is a constant.

Even if the constant will only be used - read - in the current class and/or in only one place, it's good practice to declare all constants as final: it's clearer, and during the lifetime of the code the constant may end up being used in more than one place.

Furthermore using final may allow the implementation to perform some optimization, e.g. by inlining an actual value where the constant is used.

Finally note that final will only make truly constant values out of primitive types, String which is immutable, or other immutable types. Applying final to an object (for instance a HashMap) will make the reference immutable, but not the state of the object: for instance data members of the object can be changed, array elements can be changed, and collections can be manipulated and changed.

2 of 14
12
  1. Static means..You can use it without instantiate of the class or using any object.
  2. final..It is a keyword which is used for make the string constant. You can not change the value of that string. Look at the example below:

      public class StringTest { 
               static final String str = "Hello"; 
      public static void main(String args[]) { 
               // str = "world"; // gives error 
               System.out.println(str); // called without the help of an object                       
               System.out.println(StringTest.str);// called with class name  
                 } 
             } 
    

Thanks

🌐
Medium
medium.com › @hadiyolworld007 › static-final-and-this-in-java-explained-once-and-for-all-0c2db7bd7f2a
🧩 static, final, and this in Java — Explained Once and for All | by Nikulsinh Rajput | Medium
July 15, 2025 - Java has keywords that seem simple, but cause confusion even for experienced developers. ... This blog is for you. A static variable or method is shared across all instances of a class.