Java static keyword - The Coding Shala

Home >> Learn Java >> Java Static Keyword

Java static keyword

In Java, a static keyword is a non-access modifier. We can use Java static keyword with blocks, variables, methods, and classes(nested classes). When we create a static member, we can access it directly from static methods without reference to any object. The main() is also declared as static so it can be accessed without any object. 

We can access static methods without creating any object. The following example explains it: 


class Main{
 static void Display() {
  System.out.println("Hello from static method");
 }
 public static void main(String[] args) {
  Display();
 }
}
Output: 


Hello from static method


In this chapter, we will discuss Java static variables, static methods, static blocks. 

Java static variables

In Java, when we declared a variable as static, then only a single copy of that variable is created and shared among all the objects that mean all the created objects access only one copy of the static variable. That is why we declared static variables as a global variable. 

Example of Java static variable

The following example explains the Java static variable: 



class Test{
 static int var1 = 10; //static variable
 int var2 = 10;
 
 Test(){
  var1++; //single copy
  var2++;
 }
 
 void Display() {
  System.out.println("static variable "+var1);
  System.out.println("non static variable "+var2);
 }
}
class Main{
 public static void main(String[] args) {
  Test t1 = new Test();
  System.out.println("for object1");
  t1.Display();
  Test t2 = new Test();
  System.out.println();
  System.out.println("for object2");
  t2.Display();
 }
}
Output: 
for object1
static variable 11
non static variable 11

for object2
static variable 12
non static variable 11


Note: In Java, we only can create a static method at class-level only. If we try to create a static local variable it will give a compile error. The following example explains it: 


class Test{
 static int var1 = 10; //static variable
 void Display() {
  static int var2 = 12; //it will compile error
 }
}
class Main{
 public static void main(String[] args) {
  Test t1 = new Test();
  t1.Display();
 }
}


Java static methods

In Java, if a method is declared with a static keyword then it is known as a static method. The main use of a static method is we can call it directly from other static methods we don't need to create an object for it. The static method also can access directly static data(variables). The main() method is also a static method. 

Example of Java static method

The following example explains the Java static method: 



class Test{
 static int var1 = 10;
 int var2 = 30;
 
 static void Display() {
  System.out.println("Hello from static method");
 }
 
 void msg() {
  Display();
  System.out.println("static variable "+ var1);
  //System.out.pritln("it will give error "+var2); //error non static in static method
 }
}

class Main{
 public static void main(String[] args) {
  Test t = new Test();
  t.msg();
 }
}
Output: 


Hello from static method
static variable 10


Java static blocks

In Java, we can declare a static block that will execute exactly once when the class is first loaded.  

Example of Java static blocks

The following example explains the Java static block: 


class Main{
 static {
  System.out.println("Inside the static block");
 }
 public static void main(String[] args) {
  System.out.println("Inside the main method");
 }
}
Output: 


Inside the static block
Inside the main method


Note: The code inside the static block is executed only once when the static member or class object is created. The Java static block is executed before the constructor also. The following example explains it : 


class Test{
 static int var1;
 
 Test(){
  var1++;
  System.out.println("Inside constructor");
  System.out.println("Value of var1 is "+var1);
 }
 
 static {
  var1 = 10;
  System.out.println("Inside static block");
  System.out.println("Value of var1 is "+var1);
 }
}

class Main{
 public static void main(String[] args) {
  Test t1 = new Test();
  System.out.println();
  Test t2 = new Test();
  }
}
Output: 


Inside static block
Value of var1 is 10
Inside constructor
Value of var1 is 11

Inside constructor
Value of var1 is 12


In the above example when we created the second object the static block does not get executed. 

Point to Remember: If we want to execute some code every time an object is created then we use Java Initializer Block. 

Java Initializer Block

Java Initializer block contains some code and it always executed every time an instance is created. The Java Initializer block also executed before every constructor. The following example explains it: 


class Test{
 Test(){
  System.out.println("This is constructor");
 }
 
 //initializer block
 {
  System.out.println("This is Java Initializer block");
 }
}

class Main{
 public static void main(String[] args) {
  Test t1 = new Test();
  System.out.println();
  Test t2 = new Test();
  }
}
Output:
This is Java Initializer block
This is constructor

This is Java Initializer block
This is constructor


Java static class

In Java, classes can also be made static but only nested classes can be static. If a class contains another class then the inner class is called static class. The following example explains it: 

class Test{
 static {
  System.out.println("Class Test Outer class");
 }
 
 static class Inner{
  static {
   System.out.println("Static inner class ");
  }
 }
}

class Main{
 public static void main(String[] args) {
  Test t1 = new Test();
  Test.Inner in = new Test.Inner();
  }
}
Output:
Class Test Outer class
Static inner class 



Other Posts You May Like
Please leave a comment below if you like this post or found some error, it will help me to improve my content.

Comments

Popular Posts from this Blog

Shell Script to find sum, product and average of given numbers - The Coding Shala

Shell Script to Create a Simple Calculator - The Coding Shala

Add two numbers in Scala - The Coding Shala

New Year Chaos Solution - The Coding Shala

Richest Customer Wealth LeetCode Solution - The Coding Shala