【问题标题】:NetBeans running Java program with main in non-public classNetBeans 在非公共类中运行带有 main 的 Java 程序
【发布时间】:2014-08-28 12:55:34
【问题描述】:

我知道有很多这样的问题,但在所有问题中,答案都是“不能在非公共类中使用 main 方法运行 Java 程序”。 (What if main method is inside "non public class" of java file?)

但是,我在 NetBeans 中尝试了这种情况,并且运行得非常好。为什么?

main 是否在公共类a 约定或严格规则中?

【问题讨论】:

  • this 不回答你的问题吗?

标签: java netbeans main


【解决方案1】:

rule 如下

方法main 必须声明为publicstaticvoid。它必须 指定一个声明类型为数组的形式参数(第 8.4.1 节) String.

但是对封闭类的可访问性没有限制。但是请注意,顶级类不能是privateprotected。也许这就是你产生困惑的地方。

你可以拥有

class Example {
    private static class Other {
        public static void main(String[] args) throws Exception {
            System.out.println("main in Other");
        }
    }
}

并执行

> java Example$Other

那会显示

main in Other

我不知道你为什么,但你可以

【讨论】:

    猜你喜欢
    • 2012-09-13
    • 1970-01-01
    • 1970-01-01
    • 2015-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多