Static variable:
If we want to share the value of any field or variable inside all the instances of class then we should declare that variable as static.
In above diagram we see that e1 and e2 are two objects of class Employee. they share the static variable companyName.
Static variables are also called as Class Variables because its copy gets shared between all the objects of same class.
Static variable gets space in method area at the time of class loading.
We can access static variables using object reference but it is designed to access using class name and dot operator.
Example:
Employee employee=new Employee(); object creation
System.out.println(“Company name:”+Employee.CompanyName): ok
System.out.println(“Company name:”+employee.CompanyName): Ok
Comments
Post a Comment