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 can not 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 belong to the Class rather than to any particular instance.

How to Invoke

Static variable and Methods can be used without having any 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 any 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 can not access an instance variable.
  • Static Methods can not 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 execute 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.
Series Navigation<< this keyword in JavaInterface in java >>
By | 2017-07-09T12:11:23+00:00 April 19th, 2012|Core Java|31 Comments

About the Author:

31 Comments

  1. venja November 26, 2013 at 2:59 am - Reply

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

  2. Raga December 17, 2013 at 4:50 pm - Reply

    This site is helped me a lot to as a beginner , every thing looks so simple to understand .

    Thanks a lot 🙂

  3. Kamal Kumar February 17, 2014 at 11:27 am - Reply

    Although i get some good points here. therefore i would like to publish this site….

    • gggg January 11, 2017 at 2:57 pm - Reply

      Good Note !!

  4. Suraj July 1, 2014 at 11:00 am - Reply

    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 ?

    • Vivekanand Gautam July 2, 2014 at 6:02 pm - Reply

      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

      • Sandeep June 2, 2016 at 6:48 am - Reply

        Hi VIVEKANAND GUATAM
        Very good answer..

  5. Ramu July 25, 2014 at 6:21 am - Reply

    i have 3 years exprinc ,j2ee ,spring,hibernate,jsf,corejava,css,html

    • arjun January 16, 2015 at 8:51 am - Reply

      so what ??????? this shows how much u have

  6. Gaurav Singh September 6, 2014 at 6:50 pm - Reply

    can we use static and final keyword together ?

    • Vivekanand Gautam September 7, 2014 at 6:32 am - Reply

      You can use final with static.

  7. sindhu December 13, 2014 at 11:40 am - Reply

    Vivekanand Gautam garu thank you for providing this website .am really satisfied these answers .thank-you

  8. infoj May 17, 2015 at 6:26 pm - Reply

    Now interfaces can have static methods too.

  9. Hari Prasad June 6, 2015 at 10:38 am - Reply

    Its a really very good website.I have a lot of Things through this website.thanq u so much

  10. Raman December 8, 2015 at 7:35 am - Reply

    Can we use static for package level also i.e static import..

    • J Singh December 18, 2015 at 6:05 pm - Reply

      you cannot use static with package import.

      • hui March 26, 2016 at 3:32 pm - Reply

        you can now use static with package import after jdk1.8

  11. faiza January 2, 2016 at 2:08 pm - Reply

    Salam nice study

  12. INTRADER January 27, 2016 at 10:47 pm - Reply

    NICELY EXPLAINED

  13. sammath February 2, 2016 at 10:04 pm - Reply

    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

    • J Singh February 3, 2016 at 4:56 pm - Reply

      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.

      • sammath February 8, 2016 at 9:56 pm - Reply

        Sorry it was my bad, that is what i did exactly-override in child class. thank you very much!

  14. Brijesh March 15, 2016 at 10:47 am - Reply

    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”);
    }
    }

    • Nikunj June 24, 2016 at 6:00 am - Reply

      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

  15. Hemanth Bollamreddi July 5, 2016 at 2:44 pm - Reply

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

  16. Drew August 2, 2016 at 1:53 pm - Reply

    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?

    • J Singh September 3, 2016 at 10:13 am - Reply

      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

  17. Eric Kawi Kaumbuthu April 30, 2017 at 5:37 pm - Reply

    This article has helped me understand the use of static keyword.thanks for sharing

  18. Anurag Singh July 28, 2017 at 11:07 am - Reply

    static keyword is very important for any java interviews so thank for this valuable information.
    thanks

  19. Penmatsa September 6, 2017 at 9:53 am - Reply

    Well explanation, in simple words

Leave A Comment