VARIABLES:
- In Java there are two types of variable
-
Global Variable
-> Static Variable
-> Non-Static Varible Local Variable
Static variable:
A static variable in Java is a class-level variable that is declared using the static keyword
Static variable is used to store the class information.
Unlike instance variables, only one copy of a static variable is created and shared among all objects of the class, making it useful for storing common data.
Public class Super{
Static string name="Murugan";
Static int addno=108;
public static void main(String[] args){
System.out.println(Super.name);
System.out.println(Super.addno);
}
}
Output:
Murugan
108
Static variables take single memory space for the whole class
OBJECT:
In Java Object is a instance of a class
It represents a specific entity created from the class template.
Public class Super{
Static string name="Murugan";
String pro_name="Laptop";
int price=25000;
Static int addno=108;
public static void main(String[] args){
Super product=new Super(); ---------------> This is the object creation
System.out.println(Super.name);
System.out.println(Super.addno);
}
}
In Object creation it will allocate the space for the some product in the supermarket
Object is a Non- Static Information used to store the object information
What is field ?
- In java field is a global variable
if we assign variable in the main method is a local variable otherwise it is a global variable
- In java we doesnot assign any value for the variable means it will take a default value as 0
String=null
int=0
double=0.0
boolean=false
example:
Public class Super{
Static string name= "Murugan"
string pro-name;
int addno;
Public Static void main (String[] args){
Super product1=new Super();
Super product2=new Super();
product1.pro_name="Boost";
product1.price=250;
product2.pro_name="soap";
product2.price=300;
}
}
Output:
Boost
250
soap
300
When we create a object means it will take one memory space if we create another object means it will take another memory space.
so non-static variabes are the object information
Static variables are the class information

Top comments (0)