Static Keyword – Static Variable and Static Method in Java

The static variable in Java is use for memory management mostly. We can use static keyword with variables, methods, nested classes. In this tutorial, will discuss about Static Variable and Static Method only

What is Static Variable?

If any variable is, declare static, then it calls as static variable. The static variable gets memory only once in the class area at the time of class loading

Assume in a college, there are 250 students. Each student has unique name & roll no, so when the objects are create all the instance data members will get the memory. However, the college name is same for all the students. Then we do not need to allocate memory 250 times for the students. Therefore, it is advisable to make the college static variable that means the variable creates once and used by all students.

public class Student {
    int RollNo;
    String Name;
    static String College = "Trinity"; // Static Variable
 
     Student(int r, String n) // Constructor
     {
           RollNo = r;
           Name = n;
      }
 
      void DisplayInformation() {
           System.out.println("Roll_No: " + RollNo + ", Name: " + Name + ", College: " + College);
      }
 
      public static void main(String[] args) {
                        
         Student S1 = new Student(111, "Tom");
         Student S2 = new Student(222, "Terry");
         S1.DisplayInformation();
         S2.DisplayInformation();
  }
}
Output
Roll_No: 111, Name: Tom, College: Trinity
Roll_No: 222, Name: Terry, College: Trinity

What is Static Method

If any method is declare static, then it is called static method. Static method belongs to class not to the object of the class. 

Static method can be accesse without creating the object of the class.

Static method can access data member and can change the value of it.

public class Student {
    int RollNo;
    String Name;
    static String College = "Trinity"; // Static Variable
 
     Student(int r, String n) // Constructor
     {
          RollNo = r;
          Name = n;
     }
 
     static void change() {
          College = "CITM";
     }
 
      void DisplayInformation() {
           System.out.println("Roll_No: " + RollNo + ", Name: " + Name + ", College: " + College);
     }
 
     public static void main(String[] args) {
          change();  //Calling Static method
          Student S1 = new Student(111, "Tom");
          Student S2 = new Student(222, "Terry");
          S1.DisplayInformation();
          S2.DisplayInformation();
    }
}
 
Output
Roll_No: 111, Name: Tom, College: CITM
Roll_No: 222, Name: Terry, College: CITM

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s