Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why only 1 public class in Java file

Tags:

java

People also ask

Why there Cannot be two public classes in Java file?

So the reason behind keeping one public class per source file is to actually make the compilation process faster because it enables a more efficient lookup of source and compiled files during linking (import statements).

Can there only be one public class in a Java file?

A Java file contains only one public class with a particular name. If you create another class with same name it will be a duplicate class. Still if you try to create such class then the compiler will generate a compile time error.

How many public classes a Java file can have?

java file can contain only one public class.


It forces all Java code to be organized in a certain way, which in the long run helps improve code readability.

The Java designers chose a strict approach that enforces their idea of good design practices, and this is part of that theme. Contrast that with the anything-goes attitude in Perl.


According to this source, it is for efficient compilation :

In the sidebar it explains why: "This restriction is not yet enforced by the compiler, although it's necessary for efficient package importation"

It's pretty obvious - like most things are once you know the design reasons - the compiler would have to make an additional pass through all the compilation units (.java files) to figure out what classes were where, and that would make the compilation even slower.

The same applies also for imports of source files in IDEs. Another reason would be reasonable source sizes.


These are the rules. Although it is not quite true. You can define internal classes inside you "main" class like this:

public class A {  
   public class B {  
       ...  
   }  
}

Courtesy of Dr Heinz Kabutz and his excellent newsletter....

Why is each public class in a separate file?

This is a question that I have frequently been asked during my courses. Up to now I have not had a good answer to this question. In section 1, we read: "Although each Oak compilation unit can contain multiple classes or interfaces, at most one class or interface per compilation unit can be public".

In the sidebar it explains why: "This restriction is not yet enforced by the compiler, although it's necessary for efficient package importation"

It's pretty obvious - like most things are once you know the design reasons - the compiler would have to make an additional pass through all the compilation units (.java files) to figure out what classes were where, and that would make the compilation even slower.


We can have only one top level public either class or interface in any java compilation unit ( .java source file ).

But there can be any number of default classes/interfaces per src file.

why:

JLS leaves the option to the java compiler. And most of the compiler implementations force to have file name same as :

(1) the public class/interface name

(2) if there is a main method and no public class then any name

(3) If there is main method and public class then main method should be in that public class

(4) if there is no public class and no main method then any valid name which may or may not be matching with the class/interface names in the file.

From (2): If two public classes allowed, we should give the file two names which is terribly meaningless to file system. From (3): If two public classes allowed, we should have two main methods which is terribly meaningless to java

Hence a Java source file can have one only public class.

I think the above 4 points are forced by compiler to make the job of both compiler and jvm to find particular java source file or class file easy-so-quick for the compilation/loading/linking. Java has such built in restrictions which developers should follow to have better programming.

Source: My readings and understanding.


To understand the basic reason behind these restrictions, let's suppose compiler doesn't give compile error for not naming file name same as public class name.

Suppose there is a package A

        A
      /   \
file1.java   file2.java

file1.java

package A;

class file1
{
  public static void main(String args[])
  {

  }
}

public class file3
{
 public static void main(String args[])
 {

 }
}

Now as we know that a public class can also be accessed outside the package, now it will become the responsibility of a developer to make it accessible to the outside world. Let's see how:

Suppose package A is containing only Java files(no class files) and some class outside the package A tries to access public class file3, compiler will first try to find file3.class ( not available ), then it will try to find file3.java ( not available ). So even though file3 class is public in nature, it is not visible to the outside world. So if a compiler puts the restriction that if a file is containing public class, it should be named same as the public class name, then above issue can be resolved and the developer won't have to think of exposing public class to the outside world.

Compiler also puts the restriction that there should be atmost one public class per Java file, so that every public class can be accessed by the outside world.