Non static variables:
- Variable stores information.
- Static variables store common information of all objects.
- Non static variables store specific information of object.
- Static variables get memory only once – sharable data
- Non static variables get memory every time in object creation – non sharable.
- Non static variables get memory inside the object and initializes with default values automatically.
- We can access non static variables using object address.
public class Pro
{
int a, b;
Pro()
{
System.out.println("Object is ready");
System.out.println("a val : " + this.a);
System.out.println("b val : " + this.b);
}
public static void main(String args[])
{
new Pro();
}
}
Static variable: Declaration of variable inside the class and outside to all methods and blocks with static modifier. Get memory and initializes with default values automatically when class loads(class execution starts).
Non static variable: Declaration of variable inside the class and outside to all methods and blocks without static modifier. Gets memory and initializes with default values when object has created.
Local variable: Declaration of variable inside the block or method. We must initialize explicitly before its use.