UnicMinds

Why only one public in a Java source file

Relation between Java File Names, Class Names & Main Methods

According to Java standards and common practices, we should declare every class in its own source file. And even if we declare multiple classes in a single source file (.java), still each class will have its own .class file after compilation. For example, even if you have four classes A, B, C, and D written in one source file, once you compile that file it will result in four .class files that the JVM will read separately.

But the fact is that we can declare more than one class in a single source file with the conditions below:

  • Each source file should contain only one public class and the name of that public class should be similar to the name of the source file.
  • If you are declaring a main method in your source file then main should lie in that public class.
  • If there is no public class in the source file then the main method can lie in any class and we can give any name to the source file.

Let’s assume you named the source file as Random.java, and you didn’t have any class that is public. You have four classes and none of them are public and three of them have main methods in them. It is allowed for each of the three classes to have a public static void main method as they’re all not public.

Random.java

Why only one public in Java

When you compile using “javac Random.java”, it produces four .class files – A.class, B.class, C.class and D.class

You can run “java A.class” and correspondingly for B and C. For D, it will throw an error as there is no main method. Similarly, if you try to run “java Random”, it will throw an error because there is no Java.class file.

public classes in Java source file

Now, let’s say you have four classes in Random.java source file, but you made both class B and Class C public. Java doesn’t allow two classes to be public within a single source file. So, it will throw an error.

Below is the source file, Random.java

two public classes in Java source file

When you compile it, it will throw the error message as below

multiple public classes errors - Java

Now, let’s assume we change the source file Random.java to having only one public class as shown below.

single public in Java

And, if we then compile, we will still get an error because the file is not named as B.java

public and static main method in Java

If you name the class ‘B’  as class ‘Random’, then it will compile. But when you try to run “java Random” then it won’t run because there is no main method inside the class.

class name and source file name in Java

Why is only one class allowed to be public in a Java source file?

As soon as we execute the Java application, the JVM looks for the public class and for the “public static void main (String argos[])” within that public class. The public class acts as the initial class from where the JVM instance for the Java application begins. So when we provide more than one public class in a program the compiler itself stops you by throwing an error. This is because we’re confusing the JVM as to which class should be the initial class as only one public class with the public static void main(String args[]) is the initial class for JVM. 

Hope this is useful, thank you.

You may like to read: Tic Tac Toe program in Java, The Meaning of public static void main in Java, & Source Code vs. Object Code vs. Executable Code

BOOK A FREE TRIAL