Why only 1 public class in Java file

In any Java file, why can we have only one public class whose name matches the name of the Java file?

+42
java
Aug 26 '10 at 18:54
source share
9 answers

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

Java developers have chosen a rigorous approach that provides them with insight into good design practice, and this is part of this topic. Contrast this with the Nothing ratio in Perl.

+22
Aug 26 '10 at 19:00
source share

According to this source , it is intended for efficient compilation:

In the sidebar, this explains why: "This is not a compiler restriction, although it is necessary for efficient package import"

This is pretty obvious - like most things when you know the design reasons - the compiler will have to go through all the compilation units (.java files) to find out which classes were there, and this will make compilation even slower.

The same applies to importing source files into the IDE. Another reason is the reasonable size of the sources.

+14
Aug 26 '10 at 18:58
source share

These are the rules. Although this is not entirely true. You can define inner classes within you of the "main" class as follows:

public class A { public class B { ... } } 
+8
Aug 26 '10 at 19:02
source share

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

Why is every open class in a separate file?

This is the question I have. I was often asked during my courses. So far I have not had a good answer to this question. In section 1, we read: "Although each Oak compilation unit can contain several classes or interfaces, no more than a class or compilation unit can be made public."

In the sidebar, this explains why: "This is not a compiler restriction, although it is necessary for efficient package import"

This is pretty obvious - like most things when you know the design reasons - the compiler will have to go through all the compilation units (.java files) to find out which classes were there, and this will make compilation even slower.

+3
Feb 25 '11 at 10:12
source share

Java uses this convention to find the class / interface bytecode, starting with the class path and looking at the package hierarchy in subdirectories. The file system representation of this hierarchy also applies some basic rules.

  • Any two Java classes or interfaces in the same package cannot have the same name. File names conflict.
  • Any two Java packages in the same parent package could not have the same name. Folder paths will conflict.
  • A class is visible to all classes in one package without changes to the class path.
+1
Aug 26 '10 at 19:17
source share

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

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

why:

JLS leaves the java compiler option. And most compiler implementations force you to have a file name just like:

(1) public class / interface name

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

(3) If there is a main method and an open class, then the main method should be in this open class

(4) if there is no public class and no main method, then any valid name that may or may not match the class / interface names in the file.

From (2): If two public classes are acceptable, we must provide the file with two names that are terribly pointless for the file system. From (3): If two public classes are acceptable, we should have two main methods that are terribly pointless for java

Therefore, a Java source file can have only one public class.

I think that the above 4 points forced the compiler to do the work of both the compiler and jvm in order to find a specific java source file or class file, simple and quick to compile / load / link. Java has such built-in restrictions that developers must adhere to in order to have better programming.

Source: My readings and understanding.

+1
Feb 07 '14 at 10:26
source share

It allows you to more efficiently search for source files (.java) and compiled (.class) files at compile time (import directive) and more efficient loading of classes at runtime. The idea is that if you know the name of the class, you know where it should be found for each entry in the class path. No indexing required.

0
Aug 26 '10 at 19:05
source share

I think this may be a possible reason. There can only be one public class in a java file, because the name of the java file is the same as the name of the public class. And, obviously, we cannot have a file with two different names.

0
Feb 09 '17 at 11:28
source share

To have an understanding between the compiler and the programmer. The rule is that the source code must have the most open public class, and this class must contain the main function. Thus, without any confusion / limitation, the compiler can access the (public) class and the name of the class name for the class file. Just as this class contains main (), executing the class file will give the correct stream

0
May 25 '17 at 6:50
source share



All Articles