Skip to content

Commit 726dc37

Browse files
committed
feat: Document differences between static and final keywords in Java with variables, methods, and classes
WHAT the doc covers: - **static keyword** - Applies to: variables, methods, inner classes, and blocks. - Purpose: binds the member to the class rather than to any specific object. - Behavior: - Static variables: shared across all instances of the class. - Static methods: callable without object creation; can only access static members. - Static blocks: executed once when the class is loaded, often used for initialization. - Example: ```java class Example { static int count = 0; // shared across all instances static void increment() { // can be called as Example.increment() count++; } } ``` - **final keyword** - Applies to: variables, methods, and classes. - Purpose: makes something immutable or non-overridable. - Behavior: - Final variable: value cannot be reassigned once initialized. - Final method: cannot be overridden by subclasses. - Final class: cannot be extended. - Example: ```java final int MAX_VALUE = 100; // constant final void show() { // cannot be overridden System.out.println("Final method"); } final class MyClass { // cannot be subclassed // implementation } ``` WHY this matters: - `static` controls **scope and lifetime**, allowing memory-efficient shared state and utility methods. - `final` enforces **immutability and stability**, preventing accidental changes, ensuring contracts, and increasing program safety. - Both keywords are fundamental in designing reliable APIs, constants, utility classes, and secure inheritance hierarchies. DIFFERENCES (key interview points): 1. **Purpose**: - `static`: member belongs to the class itself. - `final`: member cannot be changed/overridden/extended. 2. **Variables**: - `static`: one shared copy for all objects. - `final`: once assigned, cannot be reassigned. 3. **Methods**: - `static`: invoked without an object; cannot use `this` or access instance members. - `final`: cannot be overridden in subclasses. 4. **Classes**: - `static`: denotes nested static class (accessed without outer instance). - `final`: class cannot be subclassed. Short key: java static-vs-final variables methods classes immutability class-level-members. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent af362de commit 726dc37

File tree

1 file changed

+5
-9
lines changed

1 file changed

+5
-9
lines changed

Section16StaticFinal/src/static and final.txt

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
The keywords `static` and `final` in Java are both used to define certain characteristics for variables, methods, and classes, but they have different purposes:
1+
The keywords `static` and `final` in Java are both used to define certain characteristics for variables, methods, and
2+
classes, but they have different purposes:
23

34
1. `static`:
45

5-
- Applies to: Variables, Methods, and Inner Classes
6-
- Purpose: It means that the variable or method belongs to the class, rather than instances of the class (i.e., it is shared by all instances of that class).
6+
- Applies to: Variables, Methods, and Inner Classes
7+
- Purpose: It means that the variable or method belongs to the class, rather than instances of the class (i.e., it is shared by all instances of that class).
78

89
- Key Points:
910

@@ -22,7 +23,6 @@ The keywords `static` and `final` in Java are both used to define certain charac
2223
}
2324

2425
2. `final`:
25-
2626
- Applies to: Variables, Methods, and Classes
2727
- Purpose: It is used to indicate that something cannot be modified.
2828

@@ -46,21 +46,17 @@ The keywords `static` and `final` in Java are both used to define certain charac
4646
Differences:
4747

4848
1. Purpose:
49-
5049
- `static` is used to indicate that something belongs to the class, not to instances.
5150
- `final` is used to make something constant or immutable (cannot be changed).
5251

5352
2. Effect on Variables:
54-
5553
- A `static` variable is shared among all instances of the class.
5654
- A `final` variable cannot be reassigned once its value is set.
5755

5856
3. Effect on Methods:
59-
6057
- A `static` method can be called without creating an instance of the class and can only access other static members.
6158
- A `final` method cannot be overridden by any subclass.
6259

6360
4. Effect on Classes:
64-
6561
- A `static` class is a nested class inside a class that can be accessed without an instance of the enclosing class.
66-
- A `final` class cannot be extended or subclassed.
62+
- A `final` class cannot be extended or subclassed.

0 commit comments

Comments
 (0)