静态内部类中的主要方法。 [英] Main method in a static inner class.?

查看:96
本文介绍了静态内部类中的主要方法。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解到Java文件中唯一的公共类也必须具有main方法。但是,在下面你可以看到内部类中的main方法而不是?
关于源文件中主要方法定义的规则是什么?

I've learnt that the only public class in a Java file must also have the main method. However, below you can see the main method inside an inner class instead? What is the rule with regard to the main method definition in a source file?

public class TestBed {
    public TestBed() {
        System.out.println("Test bed c'tor");
    }

    @SuppressWarnings("unused")
    private static class Tester {
        public static void main(String[] args) {
            TestBed tb = new TestBed();
            tb.f();
        }
    }

    void f() {
        System.out.println("TestBed::f()");
    }
}


推荐答案

如果你想用java(Java启动器:java test.MyClass)启动一个类,那么这个类必须有一个带有众所周知签名的main方法。

If you want to start a class with java (the Java launcher: java test.MyClass) then this class must have a main method with the well known signature.

您可以在任何地方使用相同签名的主方法。但是不要指望发射器会找到它。

You can have a main method with the same signature anywhere you want. But don't expect that the launcher will find it.

P.S。该语言的名称是Java,而不是JAVA。

P.S. The name of the language is Java, not JAVA.

有一个小细节:

你可以做这个:

package test;

public class Test {

    /**
     * @param args the command line arguments
     */
    static public class A {

        public static void main(String[] args) {
            System.err.println("hi");
        }
    }
}




java test.Test $ A

java test.Test$A

但这是非标准的......

but this is non standard ...

这篇关于静态内部类中的主要方法。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆