Instantiating object from inside the main of that class in Java

穿精又带淫゛_ 提交于 2019-11-27 01:56:35

问题


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

class Student {
    private String name;
    public int averageGrade;


    public Student(String n, int avg) {
        name = n;
        averageGrade = avg;
    }

    public static void main(String[] args) {
        Student s = new Student("John", 9);
    }
}

I find it confusing that they are instantiating an object from the main of the same class. Is this considered bad practice? Will the newly created object s have a main method?

Thank you!


回答1:


There's nothing wrong at all with this. It's entirely normal. (Admittedly it would make more sense for a class with a main method to be something one could obviously execute - a main method in a Student class doesn't make as much sense.)

Objects don't really have methods - classes have methods, either static methods which are called without any particular context, and instance methods which are called on a particular object of that type (or a subclass).

While you could call s.main(...) that would actually just resolve to a call to the static method Student.main; you shouldn't call static methods "via" expressions like this, as it's confusing.




回答2:


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.




回答3:


This is just fine.

I know it looks a bit recursive, but what happens is that the main() method gets called when you launch this class from the command line, and then the constructor gets called when you actually instantiate an instance of the object. (See Jon's comment as well.)




回答4:


It is neither bad nor good.

It depends on the usage. In your example the constructor is called from main() method that is static by definition, so you have no other choice.

Yet another example of "good" usage of this pattern is factory method pattern. Enums use this technique too in valueOf() method (that is an example of factory method too).




回答5:


It's totally fineIt's totally fine... If a member of a class is declared as static,it's an entity lives differently of any particular object of the class.Its something that can be used by itself,without using any objects. OR it's common between different objects.You can actually count the number of objects created from a class,by setting up a static variable in the class like

 class   A
     {
         A()
          {
           count++
           }

     static count=0;
       --- 
       ---
      }

And each time an object of A,created count will add one.

Since static methods does not belong to any objects particularly,Outside the class,its called as classname.method() just like ordinary methods are called as classObject.method()



来源:https://stackoverflow.com/questions/7892622/instantiating-object-from-inside-the-main-of-that-class-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!