Java Static Keyword

This entry is part 17 of 34 in the series Core Java Course

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.

Output of the above program

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

28 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. Raga

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

    Thanks a lot :-)

    Reply
  3. 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
  4. sindhu

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

    Reply
  5. 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
      1. sammath

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

        Reply
  6. 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
  7. 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
  8. 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

Leave A Comment

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