Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a java file have more than one class?

Tags:

java

class

People also ask

Can you have 2 classes in a file Java?

No, while defining multiple classes in a single Java file you need to make sure that only one class among them is public. If you have more than one public classes a single file a compile-time error will be generated.

Can you only have one class per file in Java?

This is false. It is possible to have arbitrarily many public classes in the same source file. It's just not possible to have more than one top-level class in the same file.

Can we have two classes in the same file?

Yes, it can. However, there can only be one public class per . java file, as public classes must have the same name as the source file. One Java file can consist of multiple classes with the restriction that only one of them can be public.

How many classes are allowed in Java?

There are 5,000 or so classes built-in to Java, and programmers have written hundreds of thousands if not millions of their own.


Yes, it can. However, there can only be one public top-level class per .java file, and public top-level classes must have the same name as the source file.

The purpose of including multiple classes in one source file is to bundle related support functionality (internal data structures, support classes, etc) together with the main public class. Note that it is always OK not to do this--the only effect is on the readability (or not) of your code.


If you want to implement a public class, you must implement it in a file with the same name as that class. A single file can contain one public and optionally some private classes. This is useful if the classes are only used internally by the public class. Additionally the public class can also contain inner classes.

Although it is fine to have one or more private classes in a single source file, I would say that is more readable to use inner and anonymous classes instead. For example one can use an anonymous class to define a Comparator class inside a public class:

  public static Comparator MyComparator = new Comparator() {
    public int compare(Object obj, Object anotherObj) {

    }
  };

The Comparator class will normally require a separate file in order to be public. This way it is bundled with the class that uses it.


Yes, as many as you want!

BUT, only one "public" class in every file.


A .java file is called a compilation unit. Each compilation unit may contain any number of top-level classes and interfaces. If there are no public top-level types then the compilation unit can be named anything.

//Multiple.java
//preceding package and import statements

class MyClass{...}
interface Service{...}
...
//No public classes or interfaces
...

There can be only one public class/interface in a compilation unit. The c.u. must be named exactly as this public top-level type.

//Test.java
//named exactly as the public class Test
public class Test{...}
//!public class Operations{...}
interface Selector{...}
...
//Other non-public classes/interfaces

Important points about the main method - part 1

Part 2

(Points regarding the number of classes and their access levels covered in part 2)