Skip to main content

Summary

Instance Variables​

  1. Multiple copies of memory will be allocated for multiple objects.

  2. Instance variable is related to instance (object) of the class, so when you change data of one object, it does not affect other objects.

  3. Instance variables must be accessed using a reference variable that contains the object.

  4. Ways to access instance variables:

    a) Using reference variable

    Hello h=new Hello();
    h.a=90;// VALID

    b) Directly using object reference

    new Hello().a=90;// VALID (Not recommended – no reusability)
  5. If you access an instance variable using a null reference,

    β†’ java.lang.NullPointerException occurs at runtime.

    Hello h=null;
    h.a=90;// Runtime Exception
  6. If you try to access an instance variable using class name,

    β†’ Compile-time error:

    non-static variable a cannot be referenced from a static context
    Hello.a=90;// ERROR

Static Variables​

  1. Only one copy of memory is allocated for static variables, shared across all objects.
  2. Static variables belong to the class, so changes affect all objects.
  3. Static variables can be accessed using the class name directly (object not required), but they can also be accessed using an object reference.
  4. 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

FeatureInstance VariableStatic Variable
MemoryMultiple copies (per object)Single copy (shared)
Belongs toObjectClass
AccessObject reference requiredClass name preferred
Null reference accessExceptionβœ” Works
Impact of changeOnly that objectAll objects

πŸš€ Key Takeaways​

  • Instance β†’ object-specific
  • Static β†’ class-level shared
  • Always prefer:
    • object.var β†’ for instance
    • Class.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​

  1. Instance Initialization Block (IIB)
  • Executed automatically after object creation
  1. Static Initialization Block (SIB)
  • Executed automatically when class is loaded
  1. Instance variables are usually initialized inside instance block
  2. Static variables should be initialized inside static block (but can also be initialized inside instance block)
  3. Instance block executes every time an object is created
  4. Static block executes only once when class is loaded
  5. Class is loaded only once when JVM uses it for the first time
  6. Instance variables cannot be accessed directly from static context β†’ must use object reference

Local Variables

  1. JVM does not initialize local variables with default values β†’ must be initialized before use
  2. Scope of local variable is within the block/method/constructor
  3. Local variables are not class members β†’ cannot be accessed using class name or object
  4. Local variables cannot be static
  5. Only final modifier is allowed for local variables
  6. Memory for local variables is allocated in STACK MEMORY

🧠 Quick Summary

ConceptKey Point
Static accessAlways treated as ClassName.variable
IIBRuns on every object creation
SIBRuns once when class loads
Local variablesNo default value, stack memory
Class membersOnly 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