A Single Java Program Can Contain How many Classe...

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
152582
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
152582
5
Start Learning
Topics Covered

Overview

A single Java program can contain how many classes?

Answer: There are no restrictions on the number of classes that can be present in one Java program. But each Java program should have only one class declared with public access specifier. There cannot be two public classes in a single Java program. Additionally, the name of the public class should be the same as the name of the Java file.

Ways Of Implementing Multiple Classes In A Single Java Program

In Java, multiple classes can be present in either a nested or non-nested manner. Let's understand how a Java program is compiled with nested and non-nested classes.

Nested class

  • In Java, a class within another class is called a nested class. Nested classes are used to group related classes, thus increasing the encapsulation (i.e. grouping similar data under a single unit).
  • The nested class can be declared public, private, protected, or default.

Let's create a nested class in Java where ClassA is the outer class and ClassB is the inner class.

ClassA.java :

When the above class is compiled with javac ClassA.java, it produces two class files, ClassA.class and ClassA$ClassB.class, one for the outer ClassA and another for the inner ClassB.

Multiple non-nested classes

  • We can have multiple non-nested classes in a single Java program, but there should be one public top-level class so that the compiler is aware of the starting point.
  • The name of the Java file should be the same as the name of the public class in it.

Let's create a Java program with two non-nested classes, ClassA and ClassB, where ClassA is the public class.

ClassA.java :

When the above Java program is compiled with javac ClassA.java, it produces two class files ClassA.class and ClassB.class.

For ClassB in the previous nested classes example, ClassA$ClassB.class file is generated because ClassB is nested inside ClassA. But in the example, the compiler generated ClassB.class because ClassB is a non-nested class.

Example Of Using Two Classes In A Java Program

Nested class

We created a nested class with OuterClass and InnerClass each with a method print().

OuterClass.java :

Main.java :

Output :

Explanation : When we run the Main class, the compiler creates an OuterClass instance and uses it to create the nested InnerClass instance.

Non-nested class

Lets create two non-nested classes ClassA and ClassB each with a method print()

ClassA.java :

Main.java :

Output :

Explanation : When the compiler executes the Main class, it creates ClassA and ClassB instances and calls their print() method.

Conclusion

  • A single Java program can contain multiple classes, and one of those classes should be declared as public
  • The name of the Java file should be the same as the name of the public class in that file.
  • Multiple classes in a Java program can either be nested or non-nested.
  • Java compiler creates a different set of .class files for nested and non-nested classes.