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

Brittany Gefroh picture Brittany Gefroh · Apr 27, 2013 · Viewed 20.2k times · Source

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.

Answer

Scott Woodward picture Scott Woodward · Apr 27, 2013

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()