class employees
{
int empno;
String name;
int emp_count;
employees(int empno)
{
this.empno=empno;
emp_count++;
}
void display()
{
System.out.println("total no.of employees attended"+emp_count);
}
}
classexecuteemployee {
public static void main(String[] args) {
employees e1=new employees(1001);
e1.display();
employees e2=new employees(1002);
e2.display();
}
}
A)
Output:
one employee appointed
one employee appointed
epno :1001
one employee appointed
epno :1002
->static variables are shared but not owned by objects (they can’t own it)
->objects can manipulate these stack variables
->to get the expected o/p make emp_count as static variable.
Static int emp_count;
Comments
Post a Comment