Skip to content

Commit 0664a60

Browse files
committed
feat: Add detailed documentation on static keyword usage, memory allocation, and behavior in Java
WHAT the doc covers: - **Definition and purpose of static keyword**: - Makes variables, methods, or blocks independent of object instances. - Shared across all objects of a class. - Example: `static int a = 5;` is accessible by all instances. - **Where static can be used**: - Static variables: single copy shared by all objects. - Static blocks: executed once when the class is loaded. - Static methods: callable without creating an object; limited to static members. - Static nested classes: inner classes that can exist without an outer class instance. - **Memory allocation**: - Class loading process explained: 1. Compilation creates `.class` file. 2. JVM loads class with Class Loader System. 3. Static variables initialized and static blocks executed during class load. - Static variables live in class-level memory, not per object, making them object-independent. - **Static vs instance variables**: - Static variables → belong to the class; changes are reflected across all objects. - Instance variables → belong to each object; changes affect only the specific instance. - Demonstrated with Calc example: - Modifying obj1.stA changes it for obj2. - Modifying obj1.inB doesn’t affect obj2’s inB. - **Static vs non-static blocks**: - Static block: executes once when class is loaded, before main(). - Non-static block: executes before constructor, every time a new object is created. - Example with Calc and Help classes showing execution order. - **Static vs non-static methods**: - Static methods: - Called with class name or object reference. - Cannot access non-static members without an object. - Useful for utility and helper methods. - Non-static methods: - Depend on object instance to be called. - Can access both static and non-static members. - Key rules: - Non-static code can freely use static members. - Static code cannot access non-static members without an object reference. WHY this matters: - Understanding static keyword is essential for: - Designing efficient memory usage with shared data. - Implementing utility classes (e.g., Math). - Handling initialization logic correctly with static blocks. - Avoiding confusion between class-level and object-level state. Short key: java static keyword memory-allocation static-vs-instance methods blocks variables. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 726dc37 commit 0664a60

File tree

1 file changed

+18
-15
lines changed

1 file changed

+18
-15
lines changed

Section16StaticFinal/src/static keyword.txt

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,37 @@
1-
Below is the generated theory and code exactly as discussed:
1+
### 1) What is a use static keyword in Java?
22

3-
---
3+
**Use of static keyword:**
44

5-
### 1) What is use static keyword in Java?
5+
When we are using the static keyword with a variable, block, or method, it becomes independent of the object.
66

7-
**Use of static keyword:**
8-
When we are using the static keyword with a variable, block, or method, it becomes independent of the object.
97
For example:
8+
109
```java
1110
class Example {
1211
static int a = 5;
1312
}
1413
```
15-
Here, `a` is not dependent on any object. If we have two objects (e.g., `obj1` and `obj2`), both objects are able to access this variable. When we use static, it becomes independent of the object.
14+
15+
Here, `a` is not dependent on any object.
16+
If we have two objects (e.g., `obj1` and `obj2`), both objects are able to access this variable.
17+
When we use static, it becomes independent of the object.
1618

1719
---
1820

19-
### 2) At which place we can use static keyword
21+
### 2) At which place we can use static keyword.
2022

2123
Four places where we can use the static keyword:
2224

2325
a) **Before variable declaration**
26+
2427
```java
2528
class Calc {
2629
static int s;
2730
}
2831
```
2932

3033
b) **Before a block**
34+
3135
```java
3236
class Calc {
3337
static {
@@ -37,13 +41,15 @@ class Calc {
3741
```
3842

3943
c) **During method creation**
44+
4045
```java
4146
class Calc {
4247
public static void add(int num1, int num2) { }
4348
}
4449
```
4550

4651
d) **With the concept of inner class** *(It is not discussed in this lecture)*
52+
4753
```java
4854
class OuterClass {
4955
int x = 10;
@@ -55,16 +61,16 @@ class OuterClass {
5561

5662
---
5763

58-
### 3) When memory get allocated
64+
### 3) When memory gets allocated:
5965

6066
For that we need to know some terms:
6167
a) Stack area
6268
b) Heap area
6369
c) Class loader system
6470

65-
**Step 1:** When you compile the code you get a `.class` file.
71+
**Step 1:** When you compile the code, you get a `.class` file.
6672
**Step 2:** When you execute (using `java MainClass`), the class is loaded by the Class Loader System.
67-
**Step 3:** During class loading, static variables are initialized and static blocks get executed.
73+
**Step 3:** During class loading, static variables are initialized, and static blocks get executed.
6874
**Step 4:** Since static variables get memory in the heap before object creation, they are independent of the object.
6975

7076
---
@@ -139,20 +145,17 @@ class Help {
139145
}
140146
}
141147

142-
/*
143148
Output:
144149
Static Block
145150
Executed before main
146151
main method
147152
Non static block executed before the execution of constructor
148153
Static block of Help class
149154
Hello
150-
*/
151-
```
152155

153156
---
154157

155-
### 6) Static method vs non static method
158+
### 6) Static method vs. non-static method:
156159

157160
- **Static methods** can be called with an object reference or class name, e.g., `ClassName.staticMethod()` or `objReference.staticMethod()`.
158161
- **Non static methods** are dependent on the object and can be called only by the object reference, e.g., `objReference.nonStaticMethod()`.
@@ -161,4 +164,4 @@ Hello
161164
i) We can use static properties or members inside non static blocks or methods without object creation.
162165
ii) But we cannot use non static properties or methods inside static blocks or methods without object creation.
163166

164-
---
167+
---

0 commit comments

Comments
 (0)