Class and Instance Memory Relationship
A class acts as a blueprint for constructing objects, meaning a single class definition can spawn numerous instances. Class definitions are stored in the Method Area, whereas the objects themselves reside in the Heap. When a variable is modified with static, it belongs exclusively to the class, not to any individual object instance.
public class Vehicle {
private String model;
private int year;
private static String fuelType;
public static void main(String[] args) {
Vehicle car1 = new Vehicle();
car1.model = "Sedan";
car1.year = 2020;
car1.fuelType = "Gasoline";
Vehicle car2 = new Vehicle();
car2.model = "SUV";
car2.year = 2023;
car2.fuelType = "Electric";
System.out.println(car1.model + " " + car1.year + " " + car1.fuelType);
System.out.println(car2.model + " " + car2.year + " " + car2.fuelType);
}
}
Output:
Sedan 2020 Electric
SUV 2023 Electric
Storage Location of Static Variables
The JVM allocates a Static Constant Pool within the Method Area. Variables marked with static are placed in this pool. If the static constant pool reaches its capacity, the variables remain within the broader Method Area. Under no circumstances are static variables stored inside the heap memory alongside object instances.
What Can Be Modified by static?
The static keyword can be applied to variables, methods, and initialization blocks.
- Variables: A static variable is a class-level variable shared among all instances of the class.
- Methods: A static method belongs to the class itself, while an instance method belongs to the object. A static method cannot directly invoke an instance method because the presence of a class does not guarantee that an object has been instantiated. Conversely, an instance method can call a static method because an object cannot exist with out its underlying class.
- Initialization Blocks: A static initialization block executes before the
mainmethod, regardless of where it is positioned in the source file. This is often utilized for pre-run configurations or optimization.
Anatomy of Object Creation
Consider the statement: Vehicle myCar = new Vehicle();
Vehicle: Defines the type of the object.myCar: The reference variable name, stored in the stacck memory.new: The operator that allocates memory space in the heap for the object.Vehicle(): The constructor that initializes the object with default or specified values.
Static and Instance Block Execution Order
public class SystemConfig {
static {
System.out.println("Static block A");
}
{
System.out.println("Instance block A");
}
public static void main(String[] args) {
System.out.println("Main method");
}
static {
System.out.println("Static block B");
}
{
System.out.println("Instance block B");
}
}
Output:
Static block A
Static block B
Main method
Because no instance of SystemConfig is created within the main method, the instance initialization blocks are never triggered during execution.
public class AppInitializer {
static {
System.out.println("Static block A");
}
{
System.out.println("Instance block A");
}
public static void main(String[] args) {
AppInitializer app = new AppInitializer();
System.out.println("Main method");
}
static {
System.out.println("Static block B");
}
{
System.out.println("Instance block B");
}
}
Output:
Static block A
Static block B
Instance block A
Instance block B
Main method
public class DataLoader {
static {
System.out.println("Static block A");
}
{
System.out.println("Instance block A");
}
public static void main(String[] args) {
System.out.println("Main method");
DataLoader loader = new DataLoader();
}
static {
System.out.println("Static block B");
}
{
System.out.println("Instance block B");
}
}
Output:
Static block A
Static block B
Main method
Instance block A
Instance block B