//Multiple.java
//preceding package and import statements
class MyClass{...}
interface Service{...}
...
//No public classes or interfaces
...
编译单元中只能有一个公共类/接口。C.u 必须精确地命名为这个公共顶级类型。
//Test.java
//named exactly as the public class Test
public class Test{...}
//!public class Operations{...}
interface Selector{...}
...
//Other non-public classes/interfaces
If you want to implement a singleton, that is a class that runs in your program with only one instance in memory throughout the execution of the application, then one of the ways to implement a singleton is to nest a private static class inside a public class. Then the inner private class only instantiates itself when its public method to access the private instance is called.
class A {
void methodDeclaration() { System.out.println("!!!"); }
}
class B {
public static void main(String[] args) {
new A().methodDeclaration();
}
}