STATIC VARIABLE IN JAVA :
- 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.
- 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);
}
}
NON-STATIC VARIABLE IN JAVA :
- It's known as non-static variables and are declared in a class outside of any method, constructor, or block.
- 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);
}
}
DIFFERENCE BETWEEN STATIC AND NON-STATIC VARIABLE :


Top comments (0)