Why JVM cannot create object of class containing main method in order to access main method from that class, if it has the name of that class?

huangapple go评论81阅读模式
英文:

Why JVM cannot create object of class containing main method in order to access main method from that class, if it has the name of that class?

问题

好的,以下是您要翻译的内容:

我不明白的是,为什么 JVM 不能创建带有 main 方法的类的对象。

如果 JVM 可以通过类的名称访问 main 方法,那么它肯定可以创建该类的对象,对吗!

但是访问主方法的背后原因是什么呢?

我之所以问这个问题,是因为如果 JVM 有类的名称来访问主方法,那么它肯定可以通过使用那个名称来创建那个类的对象。

英文:

Well, I am not understanding that why JVM cannot create object of class having main method.

If JVM can access main method by the name of that class then definitely it can create an object of that class right!

But what is the reason behind accessing main method by class name?

I am asking this question because, if JVM has the name of class to access main method then it definitely can create object of that class by using that name.

答案1

得分: 2

我认为您实际上是在问,为什么不能像这样编写您的“main”方法:

//(注意:这不起作用...)
public class Test {
    public void main(String[] args) {
        System.out.println("Hello world");
    }
}

注意:上面的main方法不是static的...

答案是:

  1. 因为 Java 并不是这样设计的... 在 1990 年代。

  2. 我们无法告诉您他们为什么以那种方式设计了 Java,因为我们不在做出决定的房间里。

  3. 我们可以推断出,目前的 Java 团队之所以没有扩展 Java 来允许您执行上述操作,是因为没有迫切的需要这样做。事实上,static 的“main”方法方式运行得很好。有一句谚语是:

    如果它没有坏掉,就不要修理它。

如果您希望通过对象作为应用程序的入口点,您可以很轻松地编写代码使其按照这种方式工作。例如:

public class Test {

    public static void main(String[] args) {
        new Test().main(args);
    }

    private void main(String[] args) {
        System.out.println("Hello world");
    }
}

这只是额外的 3 行代码。

请注意,从 Java 的规范实现的角度来看,static 的 main 方法是最简单的方法。

英文:

I think you are asking (in effect) why you cannot write your "main" method like this:

// (Note: this does not work ...)
public class Test {
    public void main(String[] args) {
        System.out.println("Hello world");
   }
}

NB: main is not static in the above ...

The answer is:

  1. Because that is not the way that Java was designed ... in the 1990's.

  2. We cannot tell you why they designed Java that way, because we weren't in the room when the decision was made.

  3. We can infer that the reason that the (current) Java team haven't extended Java to allow you to do the above is because there isn't a pressing need to do so. In fact, the static "main" method approach works fine. There is a saying that goes:

    > If it ain't broken, don't fix it.

If you want your application's entry point to be via an object, it is trivial for you to code it to work that way. For example:

public class Test {

    public static void main(String[] args) {
        new Test().main(args);
    }

    private void main(String[] args) {
        System.out.println("Hello world");
    }
}

That's just 3 lines of extra code.

Note that the static main method is the simplest approach from the perspective of the specification of Java and the implementation of Java.

huangapple
  • 本文由 发表于 2020年9月23日 16:49:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/64024255.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定