Web
Analytics
Static Variables in JAVA | Angular | ASP.NET Tutorials

For Consultation : +91 9887575540

Stay Connected :

  • if you declare any variable as static, it is known static variable.
    The static variable can be used to refer the common property of all objects (that is not unique for each object) e.g. company name of employees,college name of students etc.
    The static variable gets memory only once in class area at the time of class loading
public class emp {
	
	public int empid;
	public String empname;
	static String deptname="IT";
	static int counter=0;
	
	public static void main(String[] args)
	{
		emp obj1=new emp();
		obj1.empid=1;
		obj1.empname="yogeshdotnet";
	
		++emp.counter;
		System.out.println("Depart Name ="+emp.deptname+" Counter value is ="+emp.counter);
		
		emp obj2=new emp();
		obj2.empid=2;
		obj2.empname="sharma";
		++emp.counter;
		System.out.println("Depart Name ="+emp.deptname+" Counter value is ="+emp.counter);
		
	}
	

}