简体   繁体   中英

Static Method vs Instance Method applied to an example

Given this class

  public class Worker
private String name;
private static int WorkerCount = 0; // number of workers

public Worker(<*parameter list* >)
{
<*initialization of private instances variables* >
Worker++; //increment count of all worker
}

}

If I were to add the method...

public static int getWorkerCount()
{
return WorkerCount;}

How is the method I just added static instead of instance? I think it can be an instance method as it operates on the individual object of "WorkerCount" which is originally equal to 0.

It's more a question of who should be able to call method rather than how many objects it accesses, etc.

static fields and method belong to the Class rather than a particular instance . Thus no matter how many times you instantiate Employee , there will be exactly one int employeeCount . Each employee's reference to the employeeCount field goes back to the same location. By your use of the employeeCount field, you already get this, it's just a matter of applying the same logic to methods.

By making getEmployeeCount() static, you're saying that not only can any employee call it, the Employee class itself can call the method; you don't need an instance in order to call the method.

Employee.getEmployeeCount(); //Valid, because getEmployeeCount() is static
Employee.name; //Invalid (non-static reference from static context) because *which* employee's name?

Because it is only accessing static fields, this makes sense. No matter what instance you were to call it on, it would return the same value. Consider the code:

Employee a = new Employee();
Employee b = new Employee();

System.out.println(a.getEmployeeCount()); //2
System.out.println(b.getEmployeeCount()); //2
System.out.println(Employee.getEmployeeCount()); //2

Employee c = new Employee();

System.out.println(a.getEmployeeCount()); //3
System.out.println(b.getEmployeeCount()); //3
System.out.println(c.getEmployeeCount()); //3
System.out.println(Employee.getEmployeeCount()); //3

There is nothing preventing you from making getEmployeeCount() non-static. However, because it doesn't matter at all what instance you call the method on (and in fact you don't even really need an instance), it is both convenient and good practice to make the method static .

The method should be static to allow the employee count to be accessible at the class level.

Since employeeCount variable is static, the method body is already setup for this functionality. You likely want it this way as it keeps count of the total number of Employee objects initialized with that constructor.

Also notable is that employeeCount is a primitive value (an int), and shouldn't be referred to as an object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM