static keyword in java

Let’s learn static keyword in java.

static keyword in java

A member declared with keyword static belongs to class rather than instance of a class. static keyword can be applied to variables, block, method and nested class.

static keyword is a non-access modifier.

static keyword in java

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 static class in java with example.

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: what is 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.

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 static variable in java with example.

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. 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 of 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.

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 main method static in java?

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