Akash Pandey

Software Enthusiast and lifelong learner

Currently employed with Oracle Financial Services Software.

View My LinkedIn Profile

View My GitHub Profile

Running a java source with multiple public class files

So there is a funny behaviour around JDK 11 when we create a java source file with multiple public classes in it. Consider the following example:

//File name : MultiplePublicClass.java
public class AnotherPublicClass { 

  public void x(){
    System.out.println("Hello From AnotherPublicClass.x ");
  }

  public static void x1(){
    System.out.println("Hello From AnotherPublicClass.x1 ");
  }
  public static void main(String ... args){
    System.out.println("Hello From AnotherPublicClass.main ");
  }
}

public class MultiplePublicClassTest{

  public static void main(String ... args){
    System.out.println("Hello From MultiplePublicClassTest.main ");
    AnotherPublicClass apc = new AnotherPublicClass();
    apc.x();
    AnotherPublicClass.x1();
  }
}

Try Compiling it with javac :

> javac MultiplePublicClass.java

It will fail with following error.


MultiplePublicClassTest.java:1: error: class AnotherPublicClass is public, should be declared in a file named AnotherPublicClass.java
public class AnotherPublicClass { 
       ^
1 error

But wait for a moment here. How about we try to use new feature of Java 11 and try running the java source directly instead of compiling which writes class file to disk. Its done like this :

> java MultiplePublicClass.java

When we run like this the java launcher will compile the source in memory and will launch the First main() method it encounters irrespective of whether the first class in which the main() method is encountered is public or default access scoped.

It will produce the following output :

Hello From AnotherPublicClass.main 

If you change the class order - MultiplePublicClassTest comes first and AnotherPublicClass comes second in the source file and you repeat the process it will now execute the main() method available inside the MultiplePublicClassTest class. Following will be the output:

Hello From MultiplePublicClassTest.main 
Hello From AnotherPublicClass.x 
Hello From AnotherPublicClass.x1 

Summary