Static keyword in java

Let’s learn static keyword in java.

Java static keyword

static keyword in java

In java, keyword “static” belongs to class. A variable/block/method/inner class declared static, it belongs to class.

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

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

NOTE: ‘static’ keyword is one of the non-access modifier.

Static java example:

// java static
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


Java static variable

To declare static variable java, keyword “static” is used before variable name. Static variables are intrinsically global variables and shared amidst all objects of that class.

Also read – learn nested classes in java

At the time of class loading static variables allocate memory only once. Hence saving memory. Let’s see how to declare static variable in java,

NOTE: 

  1. static variables are memory efficient used for memory management.
  2. static variable belongs to class. It can be accessed using class name.
  3. static variable is a class level variable. Local variable can’t be static variable. How? For example,
class Demo
{
   static int number = 50;// static variable
   void print()
   {
      static int a = 100;// illegal start of expression (here we get compile time error)
   }
}


Let’s see java program to demonstrate use of static variable in java,

class Employee
{
   // instance variable
   int empID;
   String name;
   // static variable
   static String company = "Flower Brackets";
   // Employee constructor
   Employee(int ID, String n)
   {
      empID = ID;
      name = n;
   }
   // print values
   void print()
   {
      System.out.println(empID + " " + name + " " + company);
   }
}

public class StaticKeywordDemo 
{
   public static void main(String[] args) 
   {
      Employee emp1 = new Employee(101, "Virat");
      Employee emp2 = new Employee(105, "Dhoni");
      // Employee.company = "Flower Brackets"; can change company 
      // name of all objects in single line
      emp1.print();
      emp2.print();
   }
}



Output:

101 Virat Flower Brackets
105 Dhoni Flower Brackets


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. Now let’ see how to create a static method in java,

NOTE:

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

Let’s see static method java example,

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

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


When to use static variables and static methods in java?

As discussed earlier static variables are constant, that is, general to all objects and 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.