Skip to main content Skip to docs navigation
TechSpiderTutorials

Java 'static' Keyword

java static modifier

static is a modifer, and it can be used with the variables, methods and with the class.

When a member is declared static, it can be accessed before any objects of its class are created, and without reference to any object. You can declare both methods and variables to be static. The most common example of a static member is main( ). main( ) is declared as static because it must be called before any objects exist.

Class Variables (static Fields):

A class variable is any field declared with the static modifier; this tells the compiler that there is exactly one copy of this variable in existence, regardless of how many times the class has been instantiated.

A field defining the number of gears for a particular kind of bicycle could be marked as static since conceptually the same number of gears will apply to all instances. The code static int numGears = 6; would create such a static field. Additionally, the keyword final could be added to indicate that the number of gears will never change.

java static variables example

public class staticDemo1
{
  //instance variable ( non-static Field)
  int i=9000;

 //instance method (non-static method)
  public void show()
   {
   System.out.println("Number is..."+i);
   }

  //class variable (static Field)
   static int x=555;

 //class method (static method)
   public static void display()
   {
    System.out.print("statement in display..."+x);
   }
/*
Note class variable and methods can be called with the class name
 as reference.
*/

  public static void main(String args[])
   {
    //calling non-static members (instance variables and methods)
   staticDemo1 sd=new staticDemo1();
   System.out.println("value of i in main..."+sd.i); //instance variable
   sd.show(); //instance method

   System.out.println("statement in main...");

   //calling static data
   System.out.println("x value in main..."+x);
   display();

    //we are calling variable x, display() without using the classname 
   //as a reference,because x,display() are in the same class
   }
 }

Output:

value of i in main...9000
Number is...9000
statement in main...
x value in main...555
statement in display...555

Class Methods (static methods):

Methods declared as static have several restrictions:

  • They can only call other static methods.
  • They must only access static data.
  • They cannot refer to this or super in any way.

For example, if you wish to call a static method from outside its class, you can do so using the following general form:

classname.method( )

Here, classname is the name of the class in which the static method is declared. As you can see, this format is similar to that used to call non-static methods through object-reference variables. A static variable can be accessed in the same way by use of the dot operator on the name of the class. This is how Java implements a controlled version of global methods and global variables.

java static methods example

class classA
{
  static int x=5000;
  public static void demo()
   {
   System.out.println("welcome to classA...and x="+x);
   } //demo() ended
}//class endded

public class staticDemo2
{
 public static void main(String args[])
  {
   //calling static method from classA
   classA.demo();

   //calling static variable
   classA.x=9999;
   System.out.println("value of x in main..."+classA.x);
  }//main closed
}

Output:

welcome to classA...and x=5000
value of x in main...9999

Static Initialization Blocks:

A static initialization block is a normal block of code enclosed in braces, { }, and preceded by the static keyword. Here is an example:

static 
{
    // whatever code is needed for initialization goes here
}
					

A class can have any number of static initialization blocks, and they can appear anywhere in the class body. The runtime system guarantees that static initialization blocks are called in the order that they appear in the source code.

Example on Static Initialization Blocks:

public class StaticInitializationDemo
{
  static
    {
     System.out.println("WELCOME TO Java By TechSpider...");
    }

 public static void main(String args[])
  {
    System.out.println("statement in main...");
  }
  
   static
    {
     System.out.println("static block");
    }
}

Output:

WELCOME TO Java By TechSpider...
static block
statement in main...