Static Variables and Methods in Java
Static variables and methods belong to class instead of class object
If you are learning Java, you must have heard of static variables and methods. But what are static variables and methods, when you google it, you will find that same one line that
Static variables and methods belong to class instead of class object
But we don't know the story of static keyword, we don't know what actually happening at the backend, how we are able to invoke static methods without creating class objects.
Before going deep in static world, first let's understand memory management in java and understand difference between declaration and initialization.
This will help us to easily understand static variables and methods. So, let's go
Memory Management in Java
There are three memory segments
- Stack Memory:- Stores local variables and reference variables.
- Heap Memory:- Stores all created object at runtime, it stores objects with their attributes.
- Meta Space:- Contains actual compiled Java bytecode. Static members (Variables and Methods) are stored here.
Variable declaration vs initialization.
When we declare a variable, it is stored in stack memory. Example,
class StaticThings {
public static void main(String args[]) {
int a; // a is in stack memory
System.out.println(a); // It will give error.
}
}
The above code will give error because we have declared variable a but not initialized it i.e. a is not referring to any object.
It's like calling something which doesn't exist.
class StaticThings {
public static void main(String args[]) {
int a = 10; // a is in stack memory
System.out.println(a);
// output will be:- 10
}
}
Now it will work because now a is referring to an integer which is 10, and 10 is integer object which actually occupies space in memory.
Only those objects and methods can be accessed which occupies space in memory, that's what exactly happens in case static members.
Static vs non Static
There are two steps are followed by JVM when we run a java file,
- Compilation:- In this step .java file is compiled in .class file which is java byte-code.
- Interpretation:- In this step, java byte-code is executed.
Now static members are available in java byte-code, and non static members are only available when an object of that class is created i.e. when they occupy space in heap memory
For example,
public class StaticThings {
public static void main(String[] args) {
Things.Example1(); // This will give error
}
}
class Things {
static void Example() {
System.out.println("This is a static function.");
}
void Example1() {
System.out.println("This is a non static function.");
}
}
Above code will give error because, main is calling a function which doesn't exist i.e. which doesn't occupy space in memory.
public class StaticThings {
public static void main(String[] args) {
Things things = new Things();
things.Example1();
// Output will be:-
// This is a non static function.
}
}
class Things {
static void Example() {
System.out.println("This is a static function.");
}
void Example1() {
System.out.println("This is a non static function.");
}
}
This will not give error because, now Example1 () exists, it actually occupy space in heap memory.
But, below code will work,
public class StaticThings {
public static void main(String[] args) {
Things.Example();
// Output will be:-
// This is a static function.
}
}
class Things {
static void Example() {
System.out.println("This is a static function.");
}
void Example1() {
System.out.println("This is a non static function.");
}
}
This will work because, Example() already exists in Meta space, memory was allocated to Example() to at compilation time when .java was compiled to java byte-code where class and static members occupy space.
Though Object of class Things was not created but still we were able to invoke Example() because Example() already exists in Meta Space.
That's why we say static members belong to class not to class object, because they occupy memory with class at compile time.
I hope you have understood the story behind Static members, Please do share if you like it.