Skip to content

Commit 9fd2634

Browse files
committed
feat: Explain constructor limitation in initializing static variables with usage examples and best practices [static-init-explained]
📌 Highlights: - Demonstrates how static variables are class-level and initialized at class load time. - Shows why constructors are only suitable for instance variables. - Includes examples of direct static initialization and static blocks. - Emphasizes correct usage patterns for global vs object-specific data. Signed-off-by: Somesh diwan <[email protected]>
1 parent 7fc8b38 commit 9fd2634

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

Section10Methods/LocalandGlobalVariables/src/Is Constructor Used for Static and Global Variables.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class Company {
3131
System.out.println("Static block executed");
3232
}
3333
}
34+
3435
This ensures companyName is set before any object is created.
3536

3637
3. Constructor Only Initializes Instance Variables
@@ -57,4 +58,4 @@ companyName is initialized outside the constructor because it does not belong to
5758
Conclusion
5859
✅ Constructors are used for initializing instance variables because they belong to objects.
5960
❌ Constructors are NOT used for static (global) variables because static variables belong to
60-
the class and are initialized when the class is loaded. Instead, direct assignment or a static block is used.
61+
the class and are initialized when the class is loaded. Instead, a direct assignment or a static block is used.

0 commit comments

Comments
 (0)