Static Variable in Java

Static variables should declare with in the class but outside of any method or block or constructor with static keyword.
If  any field of a Class  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.

  • Static variables can be accessed by directly or by using class name.
  • Static variables are stored in Method Area.
  • Static variables value is common to  all objects. 
  • Static variables  will get default values. 
Example
File:Static_Variable_Demo.java
package com.practice;

public class Static_Variable_Demo {
 static int id = 100;// static variable declaration
 static String name = "java2study";// static variable declaration
 
//instance method 
 public void show_static_variables() {
  //inside instance method we can invoke static variables directly
  System.out.println(id);
  System.out.println(name);
  

 }

 public static void main(String[] args) {
  System.out.println(Static_Variable_Demo.id);//invoking instance variable using class name  System.out.println(Static_Variable_Demo.name);//invoking instance variable using class name
  Static_Variable_Demo static_Variable_Demo = new Static_Variable_Demo();
  static_Variable_Demo.show_static_variables();
  
  
 }

}
Output
100
java2study
100
java2study