static keyword in Java .

In this session i have discussed static keyword in Java .


The static keyword is used to save the memory space . We can declare the static variable , method , block , nested class . the static members do not belong to the instance of the class . It will get memory only once . It can be access without creating the instance of class . You can access the static members to write the class name and dot operator with static member


We can declare the static as follows .

1-Variable

2-Method

3-Blocks

4-Nested class


1-Java static variable

We can declare the variable with static qualifier , It is called the static variable . The static variable value is common for all the instance of class .

    class A{

        static int count=0;
        public void getcount(){
            
            count++;
        }
        public void putcount(){

            System.out.println("Count : "+count);
        }

        
        public static void main(String args[]){

               A obj1=new A();
               obj1.getcount();
               A obj2=new A();
               obj1.putcount();
               obj2.putcount();
        }
    }
    OUTPUT : Count : 1
             Count : 1
    

In the above program count value 1 is same for both object obj1 and obj2 because of static variable . If you do not declare static variable on that case the value for obj1 is 1 and for obj2 is 0 . In my program we call the getcount method with obj1 it will increment the static variable count value as 1 that common for both object obj1 and obj2 .

Example : Write a java program that count the number of object .

    class A{

        static int count=0;
        public A(){
            
            count++;
            System.out.println("No of object : "+count );
        }

        public static void main(String args[]){

               A obj1=new A();
               A obj2=new A();
               A obj3=new A();
        }
    }
    OUTPUT : No of object : 1
             No of object : 2
             No of object : 3
    

If we do not declare the static variable count then it will display every time no of object 1


2-Java static method

We declare a method with static qualifier . It is called the static method . We do not call the static method with object . The static method belong to class rather than object of the class . It can access the static data members and change their values .

Example : Write a java program that count the number of object and reset it .

    class A{

        static int count=0;
        public A(){
            
            count++;
            System.out.println("No of object : "+count );
        }
        public static void resetCount(){
        
            count=0;
            System.out.println("Reset the count value");
        }

        public static void main(String args[]){

               A obj1=new A();
               A obj2=new A();
               A obj3=new A();
               A.resetCount();
               A obj4=new A();
        }
    }
    OUTPUT : No of object : 1
             No of object : 2
             No of object : 3
             Reset the count value
             No of object : 1
    

Example : Write a java program that demonstrate the use of static method .

    class A{

        public static void show(){

            System.out.println("This is static method");
        }

        public static void main(String args[]){

                A.show(); // called the static method             
        }
    }
    OUTPUT : This is static method
    

3-Java static block

The static block is used to initialized the static data members . It is executed at the time of class loading . It is also known as static initializer . It is very much useful while performing the operation that required to be execute only once for the class in an application life time .

    class A{

     
        static{
            
            System.out.println("Static block get executed");
        }
        public static void main(String args[]){

            System.out.println("Main method begin");
        }

    }
    
    OUTPUT : Static block get executed
             Main method begin
    

Example : Write a java program that demonstrate the real use of static block .

import java.text.SimpleDateFormat;  
import java.util.Date;  
class A{

   static{
            
    SimpleDateFormat formatter=new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");  
    Date date = new Date();  
    System.out.println(formatter.format(date));  
   }
   public A(String name){

        System.out.println("Name : "+name);
   }
   public A(String name , String city){
        
      System.out.println("Name : "+name+" "+"City : "+city);
   }
   public static void main(String args[]){

       A obj1=new A("Ram");
       A obj2=new A("Amit","Gorakhpur");
   }
}
    
    OUTPUT : 15/03/2019 12:11:22
             Name : Ram
             Name : Amit City : Gorakhpur
    

In the above program static block print the date and time . If you will not use the static block on that case we need to write the date and time code in both constructor that means two times , It is not good practice .


4-Static nested class

We will discuss this on later topic .

Welcome to AndroidATPoint.

back image                     next image