Instantiating object from inside the main of that class in Java

前端 未结 5 980
萌比男神i
萌比男神i 2020-12-08 15:58

I was looking through my OOP class documentation and I found this example:

class Student {
    private String name;
    public int averageGrade;


    public         


        
5条回答
  •  情书的邮戳
    2020-12-08 16:17

    No, it's not bad practice. It's actually fairly frequent. What you missed is that main is a static method. It's not a method of the Student object. It's a method of the Student class. You don't invoke it with someStudent.main(...), but with Student.main(...).

    See http://download.oracle.com/javase/tutorial/java/javaOO/classvars.html for more explanations.

提交回复
热议问题