简体   繁体   English

静态内部类中的主要方法。

[英]Main method in a static inner class.?

I've learnt that the only public class in a Java file must also have the main method. 我了解到,Java文件中唯一的公共类也必须具有main方法。 However, below you can see the main method inside an inner class instead? 但是,下面您可以看到内部类内部的main方法呢? 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()");
    }
}

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. 如果要使用Java(Java启动器:java test.MyClass)启动类,则该类必须具有带有众所周知签名的main方法。

You can have a main method with the same signature anywhere you want. 您可以在任何需要的地方使用具有相同签名的main方法。 But don't expect that the launcher will find it. 但是不要期望启动器会找到它。

PS The name of the language is Java, not JAVA. PS语言的名称是Java,而不是JAVA。

There is a minor detail: 有一个小细节:

You may do this: 您可以这样做:

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 ... 但这是非标准的...

Any class that can have a static method can have a public static void main(String[] args) . 任何可以具有静态方法的类都可以具有public static void main(String[] args)

This includes: 这包括:

top-level classes (whether public or not), eg 顶级课程(无论是否公开),例如

public class Foo {
    public static void main(String[] args) { 
        System.out.println("Hello"); 
    }
}

and inner static classes (whether public or not) (like your example). 和内部静态类(无论是否公开)(例如您的示例)。

It does not include: 它不包括:

  • anonymous classes 匿名类
  • inner non-static classes 内部非静态类

So both of these are illegal: 所以这两个都是非法的:

public class Foo {
    private Object bar = new Object() {
        public static void main(String[] args) {
            System.out.println("Hello");
        }
    };
}


public class Foo {
    private class Bar {
        public static void main(String[] args) {
            System.out.println("Hello");
        }
    };
}

Every Java application must have a main method. 每个Java应用程序都必须有一个main方法。 It's the starting point for the execution of the code in the application. 这是在应用程序中执行代码的起点。 Its method signature is: 其方法签名为:

public static void main(String[] args) 

A static inner class is a class that is defined inside of a different class's definition and marked as being static. 静态内部类是在其他类的定义内定义并标记为静态的类。

For example, if the outer class is named TestBed, then an inner class of TestBed, which is named Tester, would be compiled into TestBed$Tester.class. 例如,如果外部类名为TestBed,则名为Tester的TestBed内部类将被编译为TestBed $ Tester.class。 The separation of .class files means that you can keep the supplemental, nested code tightly coupled to the primary, outer class. .class文件的分离意味着您可以将补充的嵌套代码与主外部类紧密耦合。

They are in the same source file, and the inner class is actually inside the outer class. 它们在同一源文件中,内部类实际上位于外部类中。 All that and you don't have to pay any sort of deployment or run time cost. 所有这些,您不必支付任何类型的部署或运行​​时成本。

By using static inner classes, you can add additional support functionality to your systems for capabilities such as testing, while incurring no penalties in normal, production deployment. 通过使用静态内部类,您可以在系统中添加其他支持功能以实现诸如测试之类的功能,而不会在常规生产部署中造成任何损失。

To execute the main() method of that TestBed.Tester class, 要执行该TestBed.Tester类的main()方法,

% java TestBed$Tester

This is interesting as the code will compile and run in Eclipse, but will just compile from using commmand line. 这很有趣,因为代码将在Eclipse中编译并运行,但仅使用commmand line进行编译。 When you run from eclipse it will find the static main method from within the inner class and run it. 从eclipse运行时,它将在内部类中找到静态main方法并运行它。

But when running java TestBed from the command line you will get error - Exception in thread "main" java.lang.NoSuchMethodError: main which is a valid error as you have not defined your main method in main class. 但是,当java TestBed运行java TestBed ,会得到错误- Exception in thread "main" java.lang.NoSuchMethodError: main这是有效错误,因为您尚未在main类中定义main方法。

Why would you want to define your main method in an inner class? 为什么要在内部类中定义您的main方法? Your main method should be defined in public class, this is not a rule but common practice. 您的主要方法应在公共类中定义,这不是规则,而是常见做法。

In below code I've moved the main method into outer class which works both in Eclipse & command line : 在下面的代码中,我将main方法移到了在Eclipse和命令行中都可以使用的外部类中:

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()");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM