Java Static Keyword

What is Static

Static is a Non Access Modifier.

Applicable to

The Static keyword can be applied to

  • Method
  • Variable
  • Class nested within another Class
  • Initialization Block

Not Applicable to

The Static keyword cannot be applied to

  • Class (Not Nested)
  • Constructor
  • Interfaces
  • Method Local Inner Class(Difference then  nested class)
  • Inner Class methods
  • Instance Variables
  • Local Variables

 

Purpose of Static Keyword in Java

The static word can be used to attach a Variable or Method to a Class. The variable or Method that are marked static belongs to the Class rather than to any particular instance. It is opposite to Instance variable.

How to Invoke

Static variable and Methods can be used without having an instance of the Class. Only the Class is required to invoke a static Method or static variable.

/*
 * Here we will learn to access Static method and Static Variable.
 */
public class JavaStaticExample {

	static int i = 10;

	static void method() {
		System.out.println("Inside Static method");
	}

	public static void main(String[] args) {

		// Accessing Static method
		JavaStaticExample.method();

		// Accessing Static Variable
		System.out.println(JavaStaticExample.i);

		/*
		 * No Instance is required to access Static Variable or Method as we
		 * have seen above. Still we can access the same static variable and
		 * static method using Instace references as below.
		 */
		JavaStaticExample obj1 = new JavaStaticExample();
		JavaStaticExample obj2 = new JavaStaticExample();

		/*
		 * Accessing static variable in Non Static way. Compiler will warn you
		 * with below warning.
		 *
		 * The static field JavaStaticExample.i should be accessed in a static
		 * way.
		 */
		System.out.println(obj1.i);
		// Accessing satic method using reference.
		// Warning by compiler
		// "The static method method() from the type JavaStaticExample should be accessed in a static way"
		obj1.method();
	}
}

Output of the above program

Inside Static method
10
10
Inside Static method

Note*: Static keywords can be used with variables and Methods. It is not applicable to Class.

 

Class Variables – Static Fields

Class variables also known as static fields share characteristics across all Objects within a Class. When you declare a field to be static, only a single instance of the associated variable is created, which is common to all the Objects of that Class. Hence when one Object changes the value of a Class variable, it affects all the Objects of the Class. We can access a Class variable by using the name of the Class, and not necessarily using a reference to an individual Object within the Class. Static variables can be accessed even when no Objects of that Class exists. Class variables are declared using the static keyword.

 

Class Methods – Static Methods

Class Methods, similar to Class variables can be invoked without having an instance of the Class. Class methods are often used to provide global functions for Java programs. For example, Methods in the java.lang.Math package are Class methods. You cannot call non-static Methods from inside a static Method.

Static Keyword Rules

  • Variable or Methods  marked static belong to the Class rather then to any particular Instance. 
  • Static Method or variable can be used without creating or referencing an instance of the Class. 
  • If there are instances, a static variable of a Class will be shared by all instances of that class, This will result in only one copy.
  • A static Method can’t access a non static variable nor can directly invoke non static Method (It can invoke or access Method or variable via instances).

 

Cheat-sheet

  • Static is a Non-Access Modifier.
  • The Static modifier can be applied to a variable or Method or block or inner Class.
  • Static members belong to Class only not an instance.
  • A Static Method cannot access an instance variable.
  • Static Methods cannot be overriden as they are Class specific and don’t belong to an Instance.
  • Static Methods can be redefined.
  • If a Class contains any static blocks, then that block will be executed only when the Class is loaded in JVM. Creating multiple instances does not run the static block multiple time. Only the constructor will be executed multiple time.
  • If Class.forName(“class_name“) is called then the static block of the Class will get executed.

<< this keyword in Java

33 Comments Java Static Keyword

  1. venja

    This is really a really a good site for beginners, all concepts explained nicely even dummies can understand quickly!! Thanks to Author.

    Reply
  2. Suraj

    Not Applicable to

    Static keyword can not be applied to:

    Instance Variables

    ————————————-
    in the above section mentioned like , its not applicable on “Instance Variables”.

    Where Static is applicable right ?

    Reply
    1. Vivekanand Gautam

      Hi Suraj,

      Variable can be divided in 2 different type.
      1- Static Variable (Variables which are related to class and not to Objects)
      2- Instance Variable (Variables specific to Object or Instance and not to Class)

      Points is how will you make difference between these 2 types of variables while defining in class.
      Answer is “static” keyword.
      Moment you use static keyword with any variable it becomes Static variable. It is no longer Instance Variable.

      I think now you get the point.

      Regards

      Reply
  3. sammath

    Hello ,

    I dont think that this statment is true: “Static methods can not be overriden.”
    They can, I overrided it. Or could you explain what did you mean explicitly?

    Best

    Reply
    1. J Singh

      This statement is perfectly correct and it means the same thing. Static methods can not be overriden. But you can write method with same signature in child class and it looks like you are overriding but actually you are not. It is just hiding of static method.

      Reply
  4. Brijesh

    static methods and variables can be called from non-static methods.
    For example:

    public class HelloWorld{
    public static void main(String []args){
    HelloWorld h = new HelloWorld();
    h.nonStaticMeth();
    }

    static int i = 10;
    int j = 20;

    public void nonStaticMeth() {
    System.out.println(“Non static method”);
    System.out.println(“Non static – i = ” + i);
    System.out.println(“static – j = ” + j);
    staticMeth();
    }

    public static void staticMeth() {
    System.out.println(“Static method”);
    }
    }

    Reply
    1. Nikunj

      Brihesh you have created an object and it is stated in the Static Keyword Rules of the article 4th point under braces that you can access a non static variable or can invoke a non static method via objects

      Reply
  5. Hemanth Bollamreddi

    ” A Static variable can not be directly accessed by non static Methods. ” I TRIED IT AND IT WORKS FINE PLEASE REMOVE THIS POINT.

    Reply
  6. Drew

    Under your Static Keyword Rules section you state that a static method can invoke or access Method or variable via instances. But in your Cheat-Sheet section you state a Static Method can not access an instance variable. Could you explain?

    Reply
    1. J Singh

      Hi Drew,
      Answer to your question lie with statement only. Here i am saying you can not access Instance variable directly but you can access variable through Instance variable. Hope it clarifies your doubt.

      Regards

      Reply
  7. sadiqshah

    any one asked me about reference variable with examples ….. also about static and instance variable……
    i am java begginer

    Reply

Leave A Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.