Static Keyword in Java

  • Static is a keyword used for memory management.
  • Static means single copy storage for variable or method.
  • Static keyword can be applied to variables, methods, inner class and blocks.
  • Static members belongs to class rather than instance of class.


Static in java is a keyword that indicates that the variables or functions are shared between all the instances of a particular class since it belongs to the type, not the object. It is used when the programmer wants to share the same variable or method of a class.


The variable preceded by ‘static’ keyword is ‘static variable’
static int a=10;   // variable
static void m1(){   // method 
  }

Static variable is used to refer common property of all objects of class.

  • How to access static variable?

    There are two ways to access static variable:
    1. Static variable can be accessed by Class name
      A. a ; [A is class name]
      Where A is the class name and ‘a’ is a static variable declared in that class

    2. Static variable can be accessed by object
      I have a class name called ‘Sample’. Now, we can create the object of the Sample class

      Sample h=new Sample ();
      System.out.println(h.a); //’a’ is static variable inside ‘sample’ class
                          

  • How can I access static variable in two ways?

  • We will see in the following program:

    • In the above program, we printed the value of variable by using object name and by using class name

    • Static variable gets loaded into the memory at the time of class loading

    • So, we can access static variable by reference variables as well.

    • In the above program, if we create only the reference of class like

      Staticvar s1=null;
      System.out.println(s1.i); // possible
      System.out.println(Staticvar.i); //possible
      The above example compiles and executes successfully because the static variable get loaded into the memory at the time of class loading.

    • Static variable and method doesn't belong to Object/Instance of the class since static variables are shared across all the instances of Object


Example 1


Example 2

  • Local variables cannot be declared as static else the compiler displays a modifier error [at compile time]

  • We cannot call non-static members from static members because static variables get stored into memory before object creation and non-static member get stored into memory after object creation

  • So, when we access a non-static member through a static member, it leads to a compile time error as they are not present in the memory.

  • If you apply static keyword with any method, it will be treated as static method
  • Static method belongs to a class rather than object of a class
  • Static method can be invoked without creating instance of the class
  • Static method can access static data member and can change its value
  • Static methods also load into memory before object creation

  • Static method can be accessed by nullable reference like

    Staticvar s1=null;
    s1.show();
    

  • By using non-static member we can access static members, but by using static member we cannot access non-static members

Example

  • Java's static block is the group of statements that gets executed when the class is loaded into memory by Java ClassLoader.
  • It is used to initialize static variables of the class. Mostly it's used to create static resources when the class is loaded
  • We can't access non-static variables in static block
  • We can have multiple static blocks in a class, although it doesn't make much sense
  • Static block code is executed only once when the class is loaded into memory
  • Static block always get executed first because they get stored into the memory at the time of class loading or before object creation

Example

  • In the above example, we can prove that static members have one copy storage
  • A static block cannot access non-static variables and methods

Example

  • Outer class cannot be declared as static
  • But inner classes can be used as static

  • Inner class can access their local variables and methods

Example

  • Local variable cannot be static
  • Constructor cannot be static
  • Outer class cannot be static