Static keyword in java

Today we will learn static keyword in java.

Static keyword in java

In java, keyword “static” belongs to class. A variable or block or method or nested class is declared as static, it belongs to class.

A variable or method declared as static will be same for each instance of a class.

Static variable or static methods can be called without object creation. Non-static method can’t be called using static method.

Example:

class Demo
{
   // static method 
   static void display() 
   { 
      System.out.println("in static method"); 
   }
}

public class StaticKeywordDemo 
{
   public static void main(String[] args) 
   {
      Demo.display();
   }
}

Output:

in static method


Static variable in java

To declare static variable keyword “static” is used before variable name. Static variables are intrinsically global variables and shared amidst all objects in a class.

At the time of class loading static variables allocate memory only once. Hence saving memory.

public class StaticVariableDemo 
{
   // static variables are executed in order they are present 
   static int number = displayNumber(); 

   // static block are executed in order they are present
   static 
   { 
      System.out.println("static block"); 
   } 

   // static method 
   static int displayNumber() 
   { 
      System.out.println("displayNumber method"); 
      return 626; 
   }

   public static void main(String[] args) 
   {
      System.out.println("Number : " + number); 
      System.out.println("main method");
   }
}

Output:

displayNumber method
static block
Number : 626
main method


Static method in java

We can declare a method as static by adding keyword “static” before method name. Static methods belongs to class and are accessed without creating object. For example, “main()” method.

Also read – switch statement java

Static methods can call other static methods and access static data directly.

NOTE:

  • Static method cannot refer super and this.
  • static method can change static data member value.

Let’s see java program on static methods,

class EmployeeDetail 
{
   int empNumber; 
   String empName; 
   static String company = "IBM"; 
   // static method to change static variable 
   static void change()
   { 
      company = "Infosys"; 
   } 
   // constructor initialize variable 
   EmployeeDetail(int empNo, String name)
   { 
      empNumber = empNo; 
      empName = name; 
   } 
   // printing values 
   void print()
   {
      System.out.println(empNumber + " " + empName + " " + company);
   }
}


public class StaticMethodExample 
{
   public static void main(String[] args) 
   {
      // calling change method
      EmployeeDetail.change(); 
      // create objects 
      EmployeeDetail obj1 = new EmployeeDetail(659, "Sachin"); 
      EmployeeDetail obj2 = new EmployeeDetail(660, "Virat"); 
      EmployeeDetail obj3 = new EmployeeDetail(661, "Dhoni"); 
      // calling print method 
      obj1.print(); 
      obj2.print(); 
      obj3.print();
   }
}

Output:

659 Sachin Infosys
660 Virat Infosys
661 Dhoni Infosys


Static block

Static block is used to initialize “static” data member. Static block is executed before “main()” method. It is also known as Static initialization block.

Here’s an example,

public class StaticBlockExample
{
   static
   {
      System.out.println("In static block");
   }
   public static void main(String args[])
   {
      System.out.println("In main method");
   }
}

Output:

In static block

In main method

That means static block is called before “main()” method. As you can see the output “In static block” prints first. So static block always get executed first and then “main()” method.


When should we use static methods and static variables in java?

As discussed earlier static variables are constant and used that is general to all objects. Static methods are used to change static variables.

class Employee 
{
   String name; 
   int empNo; 

   // static variable 
   static String companyName; 

   // static number for unique employee number 
   static int number = 0;

   public Employee(String name) 
   { 
      this.name = name; 
      this.empNo = setEmpNo(); 
   }

   static int setEmpNo() 
   { 
      number++; 
      return number; 
   } 

   // static method 
   static void setCompany(String name)
   { 
      companyName = name ; 
   }

   // instance method 
   void getEmployeeInfo()
   { 
      System.out.println("Name : " + this.name); 
      System.out.println("Employee no : " + this.empNo); 
      // accessing static variable 
      System.out.println("Company name : " + companyName); 
   }
}

public class StaticDemo
{
   public static void main(String[] args) 
   {
      Employee.setCompany("IBM");
      Employee emp1 = new Employee("Sachin"); 
      Employee emp2 = new Employee("Virat");
      emp1.getEmployeeInfo(); 
      emp2.getEmployeeInfo();
   }
}

Output:

Name : Sachin
Employee no : 1
Company name : IBM
Name : Virat
Employee no : 2
Company name : IBM


Why is java main method static?

Because “main” method is static there is no need of object creation. Since main method is “static” JVM calls it without creating instance.


Also read – learn nested classes in java