/*Employee.java
*
*每个对象都有类中所有实例变量的副本。某些情况下,
*只有某个特定变量的一个副本应当由类的所有对象共享。
*这时,就要使用称为静态变量的静态字段。
*静态变量表示类信息---类的所有对象共享同一数据。
*
*/
public class Employee
{
private String firstName;
private String lastName;
private static int count = 0;
public Employee(String first, String last)
{
firstName = first;
lastName = last;
++count;
System.out.printf("Employee constructor: %s %s; count = %d\n", firstName, lastName, count);
}
public String getFirstName()
{
return firstName;
}
public String getLastName()
{
return lastName;
}
//静态方法获取静态变量
public static int getCount()
{
return count;
}
}
java静态变量
最新推荐文章于 2024-05-09 17:45:56 发布