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
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

Build an AI-First Career, Master the Complete Skillset

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program
NSDC Certified

AI Forward Deployed Engineer Program

Full-stack engineering, production AI and client-facing consulting

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program

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.

Sharpen Your Fundamentals with Free Learning

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.

How Scaler Transformed Careers in Different Fields

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

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

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

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.