Introduction To Static Keyword In Java

Introduction

 
In this article, we discuss static keywords in Java.
 

Static Keyword

 
The main use of static is for memory management. It can be be used in 3 ways:
  1. Static method 
  2. Static variable
  3. Static blocks

1. Static method

 
When we use the static keyword with a method in the class it is known as a static method.
  • It can be invoked without the need for creating an instance of a class.
  • It belongs to the class rather than an object of a class.
  • It can call only other static methods and cannot call a non-static method from it.
  • It can access a static data member and it cannot access non-static data.
  • It can't refer to the "super" and "this" keywords anyway.
Example
 
In this example, we create a static method to print the value of the static variable y.
  1. class StaticMethodEx  
  2.   {  
  3.     int x; //set to zero  
  4.     static int y; //when class is loaded y is set to zero but not other object created.  
  5.     StaticMethodEx()  
  6.       {  
  7.         //Constructor incrementing static variable y  
  8.         y++;  
  9.       }  
  10.     public void showvalues()  
  11.       {  
  12.         System.out.println("x = "+x);  
  13.         System.out.println("y = "+y);  
  14.       }  
  15.     //public static void incrementvalue()  
  16.       {  
  17.         //x++;  
  18.         //  
  19.       }  
  20.   }  
  21. class OutputClass  
  22.   {  
  23.     public static void main(String args[])  
  24.       {  
  25.         StaticMethodEx s1 = new StaticMethodEx();  
  26.         s1.showvalues();  
  27.         StaticMethodEx s2 = new StaticMethodEx();  
  28.         s2.showvalues();  
  29.         //StaticMethodEx.y++;  
  30.         //s1.showvalues();  
  31.       }  
  32.   }  
Output
 
pic-1.jpg
 

2. Static variable

 
When we declare any variable in our program as static, it is known as a static variable. Some properties of static variables are:
  • It can get memory only once in the class area at the time of class loading.
  • It can be used to refer to the same property of all objects, in other words, a single copy is shared by all instances of the class (that is not unique for each obj).
  • It can be accessed directly by the class name and doesn't need an object
Example
 
In this example, sv1 is the instance of the object, in this (initial) the value of a is 1 and the value of b is 8, according to the arithmetic operations in the constructor. Now, in the next instance of the object StaticVariablesEx is sv2. Here, the value of a is 2 because of the static variable while the value of b is again 8, because of non-static. In the final instance of the object StaticVariablesEx, the a is 3 while the b is 8.
  1. public class StaticVariablesEx  
  2.   {  
  3.     private static int a = 0;  
  4.     private int b = 3;  
  5.     public StaticVariablesEx()  
  6.       {  
  7.         a++;  
  8.         b += 3;  
  9.         System.out.println(a + " and " + b);  
  10.       }  
  11.     public static void main(String args[])  
  12.       {  
  13.         StaticVariablesEx sv1 = new StaticVariablesEx();  
  14.         StaticVariablesEx sv2 = new StaticVariablesEx();  
  15.         StaticVariablesEx sv3 = new StaticVariablesEx();  
  16.       }  
  17.   }  
Output 
 
pic-2.jpg
 

3. Static block

 
A Static block is a block of statements inside a Java class and it will be executed when a class is first loaded into the JVM. A Static block:
  • has the main purpose of creating the static data member.
  • is executed before the main method at the time of classloading.
Example
 
In this example, we create three static blocks and two static methods but we are not providing any functionality for them because we create them just for clarification about "how to create static method and static block" .
  1. public class StaticBlockEx  
  2.   {  
  3.     static  
  4.       {  
  5.         System.out.println("1st static block");  
  6.       }  
  7.     public static String stString = "Static Variable";  
  8.     static  
  9.       {  
  10.         System.out.println("2nd static block and "  + stString);  
  11.       }  
  12.     public static void main(String[] args)  
  13.       {  
  14.         StaticBlockEx statEx = new StaticBlockEx();  
  15.         StaticBlockEx.stMethod2();  
  16.       }  
  17.     static  
  18.       {  
  19.         stMethod1();  
  20.         System.out.println("3rd static block");  
  21.       }  
  22.     public static void stMethod1()  
  23.       {  
  24.         System.out.println("1st static method");  
  25.       }  
  26.     public static void stMethod2()  
  27.       {  
  28.         System.out.println("2nd static method");  
  29.       }  
  30.   }  
Output
 
pic-3.jpg
 

Summary 

 
In this article, we learned about the Static keyword in Java and how we can use the static keyword with a method, variable, and blocks in Java program.
 


Similar Articles