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

Solution 1:

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. The name of the language is Java, not 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

but this is non standard ...

Solution 2:

Any class that can have a static method can have a public static void main(String[] args).

This includes:

top-level classes (whether public or not), e.g.

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

Solution 3:

Every Java application must have a main method. 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. The separation of .class files means that you can keep the supplemental, nested code tightly coupled to the primary, outer 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,

% java TestBed$Tester