Summary
Instance Variablesβ
-
Multiple copies of memory will be allocated for multiple objects.
-
Instance variable is related to instance (object) of the class, so when you change data of one object, it does not affect other objects.
-
Instance variables must be accessed using a reference variable that contains the object.
-
Ways to access instance variables:
a) Using reference variable
Hello h=new Hello();h.a=90;// VALIDb) Directly using object reference
new Hello().a=90;// VALID (Not recommended β no reusability) -
If you access an instance variable using a null reference,
β
java.lang.NullPointerExceptionoccurs at runtime.Hello h=null;h.a=90;// Runtime Exception -
If you try to access an instance variable using class name,
β Compile-time error:
non-static variable a cannot be referenced from a static contextHello.a=90;// ERROR
Static Variablesβ
- Only one copy of memory is allocated for static variables, shared across all objects.
- Static variables belong to the class, so changes affect all objects.
- Static variables can be accessed using the class name directly (object not required), but they can also be accessed using an object reference.
- Ways to access static variables:
a) Using class name (recommended)
Hello.b=90;
b) Using reference variable with null
Hello h=null;
h.b=90;// VALID
c) Using reference variable with object
Hello h=new Hello();
h.b=90;// VALID
π§ Quick Comparison
| Feature | Instance Variable | Static Variable |
|---|---|---|
| Memory | Multiple copies (per object) | Single copy (shared) |
| Belongs to | Object | Class |
| Access | Object reference required | Class name preferred |
| Null reference access | Exception | β Works |
| Impact of change | Only that object | All objects |
π Key Takeawaysβ
- Instance β object-specific
- Static β class-level shared
- Always prefer:
object.varβ for instanceClass.varβ for static
d) Directly using object reference
new Hello().b=90;
β Compiler Behaviorβ
When you refer to a static variable using anything other than class name,
the compiler internally converts it to class name:
a)Hello h=null;
h.b=90; βconvertedtoHello.b=90;
b)Hello h=new Hello();
h.b=90; βconvertedtoHello.b=90;
c)new Hello().b=90;
βconvertedtoHello.b=90;
Initialization Blocks
β Ruleβ
A class can contain only five types of members:
- Variables
- Blocks
- Constructors
- Methods
- Inner Classes
You cannot place normal Java statements directly inside a class.
β Exampleβ
class Hello{
int a;// VALID
a=10;// INVALID
}
class Hai{
int b=10;// VALID
System.out.println(b);// INVALID
}
β Important Pointsβ
- Instance Initialization Block (IIB)
- Executed automatically after object creation
- Static Initialization Block (SIB)
- Executed automatically when class is loaded
- Instance variables are usually initialized inside instance block
- Static variables should be initialized inside static block (but can also be initialized inside instance block)
- Instance block executes every time an object is created
- Static block executes only once when class is loaded
- Class is loaded only once when JVM uses it for the first time
- Instance variables cannot be accessed directly from static context β must use object reference
Local Variables
- JVM does not initialize local variables with default values β must be initialized before use
- Scope of local variable is within the block/method/constructor
- Local variables are not class members β cannot be accessed using class name or object
- Local variables cannot be static
- Only final modifier is allowed for local variables
- Memory for local variables is allocated in STACK MEMORY
π§ Quick Summary
| Concept | Key Point |
|---|---|
| Static access | Always treated as ClassName.variable |
| IIB | Runs on every object creation |
| SIB | Runs once when class loads |
| Local variables | No default value, stack memory |
| Class members | Only 5 allowed types |
π Final Takeawaysβ
- Static β class-level (shared)
- Instance β object-level
- Local β method/block-level
- JVM behavior:
- Static β load time
- Instance β object creation
- Local β runtime inside method