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

前端 未结 4 1937
夕颜
夕颜 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:30

    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.

提交回复
热议问题