Static keyword in java

Let’s learn static keyword in java.

Static keyword in java

static keyword is a non-access modifier. static keyword can be used with class level variable, block, method and inner class or nested class.

static keyword in java

NOTE:

  • static keyword can’t be used with local variable.
  • static keyword can’t be used with outer class.

static class in java

To declare a class as static, that class should be an inner class.

static inner class is a nested class which is static member of outer class.

static inner class can be accessed without using object of outer class.

Now let’s see an example on static class in java.

import java.util.*;
public class OuterClass
{
   // static class
   static class NestedClass
   {
      public void show()
      {
         System.out.println("in static class");
      }
   }
   public static void main(String args[])
   {
      OuterClass.NestedClass obj = new OuterClass.NestedClass();
      obj.show();
   }
}


Output:

in static class


static keyword in java: static variable in java

To declare static variable in java, keyword “static” is used before variable name.

Syntax:

static datatype variableName;

Static variables are intrinsically global variables and shared amidst all objects of that class and it is a class level variable.

Also read – this keyword in java

static variables gets memory only once in the class area at the time of class loading.

NOTE: 

  1. static variables is used for memory management.
  2. only class level variables can be “static” variables.
  3. static variable belongs to class not object. It can be accessed using class name.
  4. static variable is a class level variable and it is initialized when class is loaded.
  5. default values for non-static varaible and static variable are same. (int, short long: 0, float and double: 0.0, boolean: false, object reference: null).

Let’s see an example on static variable in java.

class EmployeeDetails
{
   // instance variable
   int empID;
   String empName;
   // static variable
   static String company = "FlowerBrackets";
   // EmployeeDetails constructor
   EmployeeDetails(int ID, String name)
   {
      empID = ID;
      empName = name;
   }
   void print()
   {
      System.out.println(empID + " " + empName + " " + company);
   }
}
public class StaticVariableJava
{
   public static void main(String[] args)
   {
      EmployeeDetails obj1 = new EmployeeDetails(230, "Virat");
      EmployeeDetails obj2 = new EmployeeDetails(231, "Rohit");
      obj1.print();
      obj2.print();
   }
}


Output:

230 Virat FlowerBrackets
231 Rohit FlowerBrackets


static method

We can declare a method as static by adding keyword “static” before method name.

Static method belongs to class, not object. Static methods are accessed by class name.

Also read – switch statement java

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

NOTE:

  • super and this keyword cannot be used in static.
  • “static” method can access only static data.
  • “static” methods can call only static methods.
  • like static variables static methods are used for memory management.

Let’s see example on static method in java.

public class StaticMethodExample
{
   static void print()
   {
      System.out.println("in static method.");
   }
   public static void main(String[] args)
   {
      StaticMethodExample.print();
   }
}


Output:

in static method.


static keyword in java: static block in java

Static block is used to initialize “static” data member of a class. Static block is executed before “main()” method.

Static block executes automatically when the class is loaded in memory. Here’s an example on static block in java.

public class StaticBlockExample
{
   // static variable
   static int a = 10;
   static int b;
   // static block
   static
   {
      System.out.println("Static block called.");
      b = a * 5;
   }
   public static void main(String[] args)
   {
      System.out.println("In main method");
      System.out.println("Value of a : " + a);
      System.out.println("Value of b : " + b);
   }
}


Output:

Static block called.
In main method
Value of a : 10
Value of b : 50

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

NOTE:

  • we cannot execute java program without main() method. Because it was possible till JDK 1.6. Since JDK 1.7, it is not possible to execute class without main method.

advantage of static block in java

  • static block is executed at the time of class loading. So at the time of class loading if we want to perform activity, define in static block. For example, native methods.
  • static block is used to initialize static members.

static keyword in java: Why is java main method static?

“main” method is static because there is no need of object creation. Java virtual machine calls it without creating instance.