[Java] static vs. final
Purpose
Wiki topics:
💭 · Philosophy of Spirit
[Java] static vs. final
Purpose
- static : “Create and use only one instance per class” (shared)
- final : “Cannot be changed under any circumstances” (fixed)
static
Variables or methods marked as static are loaded into memory only once, even without creating an object (using new), and are shared by all objects.
class Card {
String pattern; // Instance variable (card patterns may vary)
static int width = 100; // Static variable (all cards have the same width)
static int height = 150; // Static variable (all cards have the same height)
}
Features
- Belongs to the Class, not the object (Instance).
a) Variables belonging to an object
class Student {
String name; // Instance variable
}
// Error! (Cannot use name because no Student object has been created)
System.out.println(Student.name);
// name can be used after creating a new object
Student s1 = new Student();
s1.name = “Hong Gil-dong”;
b) Variables belonging to the class (static)
class Student {
static String schoolName = “HighSchool”; // static variable
}
// Can be used even without creating a Student object, as it exists in the Student class
System.out.println(Student.schoolName); // “HighSchool”
- All objects use the same value
- No need to create an object : Accessible via the class name
- Modifiable
class Counter {
static int count = 0; // static variable
}
// No error occurs even if modified
Counter.count = 5;
Counter.count = 10;
final
The final keyword ensures that a variable is created once and cannot be modified thereafter.
Features
finalvariable (immutable) : Once a value is assigned, it cannot be changed. (Constant).
class Order {
final int orderId;
// Must be initialized exactly once via the constructor
public Order (int orderId) {
this.orderId = orderId;
}
public void changeId() {
this.orderId = 20; // Compilation error! (Final variables cannot be reassigned)
}
}
Reference Types (Object, Array) A
finalvariable fixes the memory address but does not fix the value inside the object
final List<String> list = new ArrayList<>();
list.add("ABC"); // Possible
finalmethod (cannot be overridden) : Cannot be overridden in a child class.
class Parent {
public final void printLogo() {
System.out.println(“Example”);
}
}
class Child extends Parent {
// Compilation error! (A parent's final method cannot be overridden)
@Override
public void printLog() { ... }
}
finalclass (Cannot be inherited) : Other classes cannot inherit from this class.
final class SecuritySystem {
...
}
// Compilation error! (final classes cannot be inherited)
class CopiedSystem extends SecuritySystem { ... }
static final vs. final static
Functionally, they are 100% identical.
However, static final is more commonly used.
- Oracle Java Language Specification (JLS) recommendations
- JLS recommended modifier order:
public/protected/private->abstract->static->final->transient->volatile->synchronized->native->strictfp
- Enforcements by Code Style Checking Tools (Lint)
When using code quality tools or the default inspection features of an IDE, writing final static often triggers a Warning.
메타데이터
- post_id
- cceb99b2f3e7
- slug
- java-static-vs-final-cceb99b2f3e7
- url
- https://medium.com/@dhleee0123/java-static-vs-final-cceb99b2f3e7
- canonical_url
- https://medium.com/@dhleee0123/java-static-vs-final-cceb99b2f3e7
- author_url
- https://medium.com/@dhleee0123
- status
- ok
- fetched_at
- 2026-06-23 17:05:31