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

前端 未结 4 1930
夕颜
夕颜 2020-12-11 09:19

I am supposed to do the following:

Write a Java application (Client) program with a static method called generateEmployees( ) that returns a random list

4条回答
  •  爱一瞬间的悲伤
    2020-12-11 09:45

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

提交回复
热议问题