Skip to content

Commit 505d921

Browse files
committed
feat: Add notes on static keyword usage and restrictions in Java
WHAT the doc covers: - **Purpose of static**: - Used for memory management by creating class-level members. - Belongs to the class itself, not tied to any particular object instance. - Shared across all instances, reducing redundant copies of data. - **Where static can be applied**: - Methods → can be called without creating an object. - Blocks → executed once during class loading for initialization. - Nested classes → static nested classes don’t need an outer class instance. - **Restrictions of static methods**: - Cannot access non-static (instance) variables or methods directly, since they belong to objects. - Cannot use `this` or `super`, because no specific object context exists in a static context. - Can only: - Access static members directly. - Work with non-static members if an object reference is explicitly provided. - **Concept summary**: - Static members are directly attached to the class itself. - Useful for utility functions, constants, shared resources, and singleton implementations. WHY this matters: - Clarifies why static is critical for efficient memory usage and consistent shared state. - Helps avoid common beginner mistakes (e.g., trying to use `this` in a static method). - Prepares for designing utility classes (`Math`, `Collections`) and managing application-wide data. Short key: java static keyword memory-management restrictions no-this-or-super. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent da85251 commit 505d921

File tree

1 file changed

+16
-19
lines changed

1 file changed

+16
-19
lines changed

Section16StaticFinal/src/StaticByED.java

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,8 @@
1-
//It is used for memory management.
2-
//It can applied on methods, blocks, and nested classes.
3-
//main concpt behind static is that it belongs to the class rather than instances of the class.
4-
5-
//Static method can not use non-static data members or call non-static method directly.
6-
7-
//this and super cannot be used here.
8-
//Static is Direct attached to class.
9-
101
public class StaticByED {
11-
122
public static int count =0; //Count kar raha hai StaticByED
13-
//My aim is not access via instance, but Access via Class. Goal Class se directly access karna hai.
3+
//My aim is not access via instance, but Access via Class. Goal Class se directly accesses karna hai.
144

15-
public StaticByED()
16-
{
5+
public StaticByED() {
176
count++;
187
}
198

@@ -48,20 +37,17 @@ private void setName(String name) {
4837
Name = name;
4938
}
5039

51-
public static void getCount()
52-
{
40+
public static void getCount() {
5341
System.out.println("Total conunts: " + count);
5442
}
5543

56-
public static void main(String[] args)
57-
{
44+
public static void main(String[] args) {
5845
/*
5946
Classs ka Object bana kar access kar pa raha hu.
6047
6148
StaticByED ED = new StaticByED();
6249
ED.setAge(14);
6350
System.out.println(ED.Age);
64-
6551
*/
6652

6753
StaticByED ED = new StaticByED();
@@ -72,4 +58,15 @@ public static void main(String[] args)
7258

7359
StaticByED.getCount();
7460
}
75-
}
61+
}
62+
63+
/*
64+
It is used for memory management.
65+
It can apply on methods, blocks, and nested classes.
66+
the main concept behind static is that it belongs to the class rather than instances of the class.
67+
68+
Static methods cannot use non-static data members or call non-static method directly.
69+
70+
this and supper cannot be used here.
71+
Static is Direct attached to class.
72+
*/

0 commit comments

Comments
 (0)