In Java, Static Keyword can be used with variables, methods, nested class and blocks. Static variable or method is same for all the instances of a class.

The static keyword can be used with:

  • Method
  • Variable
  • Nested class
  • Blocks

Static Method in Java

The method which is declared using the static keyword is known as the static method. The static method belongs to the class instead of the object of the class. You can invoke the static keyword without creating an instance of a class. The static method can access the static data member and can change the value of it.

Let us see the example:

public class Student{
	int Rollno;
    String Name;
    static String college = "DU";
    
    Static void change(){
		college = "TRIAS";
    }

    Student(int r, String n){
		Rollno = r;
		Name = n;
    }
    void display (){        
		System.out.println(Rollno+" "+Name+" "+college);
    }

    public static void main(String args[]){
		Student.change();

		Student s1 = new student (1,"abc");
		Student s2 = new student (2,"xyz");

		s1.display();
		s2.display();
    }
}

Where cannot we use static method in Java?

The static method cannot be used with non static data member or call the non static method directly. And also we cannot use this and super keywords in the static context

Static Variable in Java

Variables which are declared using the Static Keyword are known as Static Variables. They can be used to refer the common property of all the objects of a Class. Static Variables In Java are not instantiated per instance or Object, but they are per Class. The memory for the static variable is stored once in the class area at the time of class loading.

What is the advantage of Static Variable in Java?

The main advantage of using the static variable is that it makes our program efficient and save the memory. They can be accessed without Object Creation If made public.

What is the use of using static variable in java?

The static variable is used when we want to store any common property for all the objects of a class, like if we want to store the Name of the College in Student Object. College Name is Same for all the Student So It makes sense to have Single Variable for all the Objects,  when we create a college name as a static variable memory will be allocated only once otherwise It wil be allocated for each and every student.

Let us have a look at the example:

public class Student {
     int Rollno;
     String Name;
     static String college= “IPU”

     Student(int r,String n){
          Rollno =r;
          Name = n;
     }
     void display(){
          System.out.println(Rollno+" "+Name+ " "+college);
     }

     public static void main(String args[]){
     Student s1 = new Student (1,"abc");
     Student s2 = new Student (2,"xyz");
     s1.display();
     s2.display();
     }
}

Static Nested Class in Java

The static nested classes are those classes which are created inside a class. They cannot access the non static data members and method; it can be accessed by the outer class name. They can access the static data members of outer class including the private.

Let us have a look on the example:

public class nested{
  static String name= “abc”;

  static class nested1{
   void display()
     {     System.out.println("The name is: "+name);
     }
  }
 
  public static void main(String args[]){
  nested.nested1 obj=new nested.nested1();
  obj.display();
  }
}

 

Static Blocks in Java

The static block is used to initialize the static data member and it is executed before the main method at the time of class loading.

Let us have a look at the example:

public class Block{

  static
   {       System.out.println("You are in static block");
   }

  public static void main(String args[])
  {
     System.out.println("Hello World…");
  }
}