Java: Calling a static method in the main() method

2019-01-20 14:51发布

问题:

I am supposed to do the following:

Write a Java application (Client) program with a static method called generateEmployees( ) that returns a random list of 10 different types of Employee objects. You could either use an array or an ArrayList to store the employee objects that will be returned. Use a for loop to populate randomly different types of employee objects with some random data. You could possibly think a range of values like 1 – 4. If random value is 1, create a HourlyEmployee object with some randomly generated data, if 2, SalariedEmployee object with some random data and so on. I would leave it to your ingenuity to generate and populate these different Employee objects. As these objects are generated add them to your data structure (array or ArrayList that you are using). Finally the method returns this data structure.

In the same application class, implement the main( ) method. Call the generateEmployees( ) static method and using a for loop print the details of each of the employee along with their earnings on the terminal window.

My generateEmployees() static method is as follows (it might not be correct... also, the data hasn't been randomly generated because I'm not exactly certain how to do that, at least as far as the first and last names are concerned.):

public static Employee[] generateEmployees()
{
    Employee[] employees = new Employee[10];
    int randomNum = 0;

    for (int i = 0; i < 10; i++)
    {
        Random random = new Random();
        randomNum = random.nextInt(4) + 1;

         switch (randomNum)
         {
            case 0:
                employees[i] = new SalariedEmployee("Bri", "Gefroh", 123, 1000);
                break;
            case 1:
                employees[i] = new HourlyEmployee("Bri", "Gefroh", 123, 12.50, 10);
                break;
            case 2:
                employees[i] = new CommissionEmployee("Bri", "Gefroh", 123, 10000, 0.05);
                break;
            case 3:
                employees[i] = new BasePlusCommissionEmployee("Bri", "Gefroh", 123, 10000, 0.05, 2500);
                break;
         }
    }

    return employees;
}

How would I call this method and use it in the main() method? Each of those four types of employees are subclasses of the Employee class, and each subclass has its own toString() method, which is what I belivee I'm supposed to be outputting.

回答1:

A static method is a class method, rather than an instance method. It's called on the class, not an instance of the class. The difference being that you can call a static method without having an instance first.

Employee.doSomething();

vs

Employee employee = new Employee();
employee.doSomethingElse();

So, if your generateEmployees() method is in the same class as your main, all you need is

 generateEmployees();

otherwise you'll need to do

 Employee.generateEmployees();

(if the Employee class contains generateEmployees()



回答2:

If the method is in the same class, you should just be able to call it like any other method:

public static void main(String[] args)
{
    Employee[] employees = generateEmployees();

    // TODO: loop through and print out...
}

Since main and generateEmployees are both static, it should work. (If generateEmployees is non-static, you'd need to create an instance of the class first).

I'd suggest having a constant array of Strings with "names" in it, and use a random number to generate an index. That should help with randomising the names a little.



回答3:

It's a static method, so ... it does not need to be accessed within the context of an instantiated object. You can just, you know, call it from your public static void main(...) method. If the class that contains your main() method is named Employee, then...

Employee.generateEmployees(); 

would do the trick.



回答4:

Like Ash stated but if you need to process the records, here is no reason to introduce extra variable just do

 public static void main(String[] args)
 {
      for(Employee employee: generateEmployees())
         print(employee); // define static print somewhere too

 }