Static in Java
Understanding significance of static in Java
Static modifier in Java spans its influence on
Type (inner class , Enums) | YES |
field | YES |
methods | YES |
Key Take Aways
Type :
- Static modifier when attached to inner class means, the inner class doesn’t hold ant reference of the outer class.
- To instantiate the inner static class , outer class object instantiation is not needed.
StaticDemo.InnerClass innerClass = new StaticDemo.InnerClass();
Field:
- Static field first take default values and then are overwritten by static initialize.
- Static fields are initialized when the class is loaded by the class loader and not at the time of Object instantiation.
- Static field of a class means it represents attribute of the class and not the object.
- Static field is not stored as part of the Object.
- Static field is saved along with the class and there is only one copy(memory allocation).
- Static field can be referenced in both static and not static methods.
- Static field is shared by all the objects of the class in the JVM . change by one will be reflected in all others as there is only one copy.
- Static field can be reference directly by Classname. Reference is not needed .
StaticDemo.getStaticMethod();
Methods:
- All methods are part of the class and none is part of the object.
- Static methods signify they represent functionality of Class and not of the object.
- Static modifier in a method signifies that the method is not accessing any instance field of the class directly.
- Static modifier in a method signifies that the method is not dependant on the object.
- Static method can only access static fields , as “this” pointer is not available with the method.
[addToAppearHere]
Code:
package com.big.data.java.modifiers;
public class StaticDemo {
// Default value if int is 0 , objects is null both for static and instance fields
private static int staticState;
static {
// Executed when class is loaded by JVM
System.out.println("Inside Static Initializer block staticState value before is " + staticState);
staticState = 10;
System.out.println("Inside Static Initializer block staticState value before is " + staticState);
// state = 20 not possible as state is part of object not class
}
private int state;
public StaticDemo() {
state = 10;
System.out.println("Inside Constructor staticState value before is " + staticState);
// Static field is accessible from non static methods and constructors
staticState = 30;
}
// all methods are part of class and not of object .
// the significance of static methods is it is not accessing any instance members directly
// Static method refer class behaviour and not object behaviour
private static int getStaticMethod() {
return staticState;
//state = 30; non static field is not accessible withing static methods
}
public static void main(String[] args) {
StaticDemo reference = new StaticDemo();
// Static method is accessible via class Name .
StaticDemo.getStaticMethod();
StaticDemo.InnerClass innerClass = new StaticDemo.InnerClass();
}
// Static innerClass signifies it will not hold the reference of the outer class.
// A non static innerClass holds the reference of the outer class
public static class InnerClass {
private int innerState;
public InnerClass() {
innerState = 20;
//state = 10; cannot access member of the outer class. as it dosent hold refrence of the outer class
System.out.println("Inside Constructor of InnerClass , innerState value is " + innerState);
}
}
}
[addToAppearHere]
Output :
Inside Static Initializer block staticState value before is 0
Inside Static Initializer block staticState value before is 10
Inside Constructor staticState value before is 10
Inside Constructor of InnerClass , innerState value is 20