DEV Community

Cover image for STATIC & NON-STATIC IN JAVA
Vinoth Kumar
Vinoth Kumar

Posted on

STATIC & NON-STATIC IN JAVA

STATIC VARIABLE IN JAVA :

  1. A class-level variable that is declared using the static keyword. Instance variables, only one copy of a static variable is created and shared among all objects of the class.
  2. It useful for storing common data.

EXAMPLE :

Public class Home{
  static string name = "RV";
  static int years = "25";
Public static void main(String[] args){
  System.out.println(Home.name);
}
}

Enter fullscreen mode Exit fullscreen mode

NON-STATIC VARIABLE IN JAVA :

  1. It's known as non-static variables and are declared in a class outside of any method, constructor, or block.
  2. Instance variables are created when an object is instantiated and destroyed when the object is destroyed.

EXAMPLE :

Public class Home{
  string name = "RV";
  int years = "25";
Public static void main(String[] args){
  System.out.println(Home.name);
}
}

Enter fullscreen mode Exit fullscreen mode

DIFFERENCE BETWEEN STATIC AND NON-STATIC VARIABLE :

Top comments (0)