In the previous tutorials, we have seen different types of Java variables such as
- Instance Variables
- Static Variables
- Local Variables
If you haven’t seen that tutorial, it is recommended to see before moving further.
This tutorial lets you have a deep understanding of java Static keyword and how do we use in our day-to-day developing environment.
Java Static:
In Java Static is a keyword used to define a specific functionality (block of execution) at a class level. Hence the static function is the class level functionality.
Since the static is a keyword, we can use this in front of :
- Variables
- Methods
- Blocks
- Inner classes
Let’s see these four usages of the static keyword in detail.
Static variables :
A variable which is declared by using the static keyword is called as a static variable. Static variables are considered to be class-level variables because the values of the static variables are at class level, and not bound to be a specific instance of that class, whereas in instance variables the value of a variable is bound to be with a specific instance.
Static variable Syntax :
public static int a;
Static variables are allocated to the memory in the static pool, and it happens while loading the class itself.
Since the static variables are class level variables, we do not need any object reference to access these variables. We can directly access these variables by using the class name itself.
The syntax for Accessing Static Variables :
ClassName.staticVariable;
Important points about static variable:
- Static variables are also called as class variables.
- Class variables belong to the entire class and not a specific instance of the class.
- Static variables are declared with the static keyword.
- A single static variable can be shared by all instances of a class.
- We can’t access the static variables from the normal methods.
- Class variables are allocated to the memory only once at the time of class loading. which can be commonly accessed by all the instances of the class.
- Static variables are allocated to the memory of the static pool.
- Since the memory for the class variables is allocated at the time of loading the class itself, we can access the static variables directly with the class name itself.
Example of Static Variables:
class SampleStaticClass {
static int x;
}
public class StaticVariableDemo {
public static void main(String[] args) {
SampleStaticClass.x = 20;
System.out.println("A.x -- " + SampleStaticClass.x);
SampleStaticClass object = new SampleStaticClass();
object.x = SampleStaticClass.x + 20;
System.out.println("ob.x -- " + object.x);
SampleStaticClass.x = object.x - 10;
System.out.println("A.x -- " + SampleStaticClass.x);
System.out.println("ob.x -- " + object.x);
SampleStaticClass object2 = new SampleStaticClass();
object2.x = (object2.x * 2) + (SampleStaticClass.x - 10 + object.x);
System.out.println("A.x -- " + SampleStaticClass.x);
System.out.println("ob.x -- " + object.x);
System.out.println("ob1.x -- " + object2.x);
}
}
Output:
SampleStaticClass.x -- 20
object.x -- 40
SampleStaticClass.x -- 30
object.x -- 30
SampleStaticClass.x -- 110
object.x -- 110
object2.x -- 110
Default Values of Static Variables :
For static variables, we don’t need to perform initialization explicitly. The JVM will provide default values to it as follows:
- In the case of primitives, the default value is zero (0).
- In the case of object and characters, the default value is ‘null’
- In the case of boolean, the default value is ‘false’.
Static Methods:
If a method is declared as static, then we can say that method is static. The extreme example of a static method in Java is the main method.
Like static variables, static methods are also called without using object reference. We can directly call the static methods from the main method since the main method also a static method.
Points about static methods:
- Static methods can be accessed directly by using the class name.
- Static methods can’t be overridden.
- Non-static methods can access static methods only by using the class name.
- Static methods can also access the non-static methods by using the instance of the class.
- Both static and non-static methods are not accessed directly.
- A static method cannot refer to “this” or “super” anywhere.
Example for static methods :
public class StaticMethodDemo {
public static void main(String[] args) {
someStaticMethod();
}
public static void someStaticMethod() {
System.out.println("Hello I am a static method ");
StaticMethodDemo methodDemo = new StaticMethodDemo();
methodDemo.sample();
}
public void sample() {
System.out.println("Normal Method ");
StaticMethodDemo.someOtherStaticMethod();
}
public static void someOtherStaticMethod() {
System.out.println("Hello I am some other static method ");
StaticMethodDemo methodDemo = new StaticMethodDemo();
methodDemo.sample();
}
}
Static block:
We have different types of blocks in Java, like initialization block, synchronized block and static block. Every block has its own importance. Here a static block is a block of statements, which is defined by using the static keyword.
Static Block Syntax:
static{
//Code
}
Like static variables and methods, static blocks are also a class level. Hence the static blocks will be executed at the time of class loading. If we want to perform any operation while loading the class, we can define that functionality (block of statements) within the static blocks.
Usage of static blocks:
Static blocks are used in:
- Registering the database drivers
- Loading the native libraries.
We can define any number of static blocks in a class and all will be executed from the top to bottom.
Points about static Block:
- Static blocks executed by the JVM.
- We cannot access the static blocks directly or indirectly.
- We cannot directly access the non-static content from the static blocks.
- Static blocks can be executed before the default constructor. The priority of the static block is high compared to the default constructor.
- We can’t change the access of static blocks (There are no public, private, protected static blocks).
Execution Flow of Static Blocks :
public class StaticBlockDemo {
static {
System.out.println("static Block 1");
}
public StaticBlockDemo() {
System.out.println("In default Constructor");
}
static {
System.out.println("static Block 2");
}
public static void main(String[] args) {
StaticBlockDemo blockDemo = new StaticBlockDemo();
}
static {
System.out.println("static Block 3");
}
}
Output:
static Block 1
static Block 2
static Block 3
In default Constructor
Can we write a Java program without main method?
Yes!! We can write a program without using the main method, but this is possible before versions of Java 1.7. In Java 1.7, JRE looks for the main method before going to load the class. If JRE doesn’t find the main it will throw the below error.
public static void main(String[] args)
In the before versions of Java, 1.7 JRE looks for the main method after executing the static blocks. But the execution and priority of the static blocks are fixed.
Static Class:
In Java, we can use the static keyword in front of a class like variables, methods and blocks.
But we have an extreme limitation while creating the static classes, that is we can only make the class as static if and only if that is an inner class. We can’t create an outer class as a static class.
What is the inner class?
A class is defined under another class is called an inner class or nested class.
A class is enclosed with the nested class is called as an outer class.
An inner class may be static or non-static. There are some functional differences between the static inner class and non-static inner class.
Static vs Non-static inner classes:
- Static inner class doesn’t need a reference to access the outer class members, whereas non-static inner class should have the reference of the outer class to access the outer class members.
- A static inner class can only access the static member of the outer class, whereas a non-static inner class can access both static and non-static members of the outer class.
Example of static and non-static inner classes :
public class InnerClassDemo {
public static void main(String[] args) {
// Accessing the Static Inner class
InnerClassDemo.StaticInnerClass innerClass = new StaticInnerClass();
System.out.println("StaticInnerClass value : " + innerClass.getValue());
// Accesing the Normal Inner class
InnerClassDemo.NormalInnerCalss normalInnerCalss = new InnerClassDemo().new NormalInnerCalss();
System.out.println("normalInnerCalss Value : "+ normalInnerCalss.getValue());
}
static class StaticInnerClass {
int a = 10;
public int getValue() {
return a;
}
}
class NormalInnerCalss {
int instVar = 20;
public int getValue() {
return instVar;
}
}
}
Output:
StaticInnerClass value : 10
NormalInnerCalss Value : 20
Happy Learning 🙂