Multiple classes in one file in Java

Multiple classes in one file in Java

In this post, we will see how to have multiple classes in one file in java.

Introduction

You need to have any number of classes in a single Java file, but there is a restriction that you can declare only one Java class as public modifier with the main() method and declare (without public) for the remaining classes. Moreover, we have one rule in Java that your filename must be the same as your public class name.

Methods to Implement Multiple Classes In One Java Program

1) Nested classes

A nested class is one type of inner class that accesses other instance variables of an outer class. We can use any access modifiers for the nested inner class such as private, public, protected, or default.

There are two types of nested classes defined in Java.
1.static nested class

  1. non-static nested class.

If You define an inner class with a static keyword, such type of class is said to be a static nested class, and an inner class is also said to be a non-static nested class in Java.

Example

output

Inside Organization class constructor

Total Organization projects: 0
Inside Project class constructor.
Total Company projects: 1
Calling private method of Organization class from Project class
Greetings from private method of Organization class
Inside Project class displayDuration method
Project duration: 10
Inside Project class constructor.
Total Company projects: 2
Calling private method of Organization class from Project classGreetings from private method of Organization class
Inside Project class displayDuration method
Project duration: 20

Explanation of the program

In the above program, we have created outer class Organization, and inside it, we have created an inner class Project*. In the output, we have decided to directly access the methods and variables of an outer class Organization from the inner class Project.


2) Multiple non-static nested classes

The non-static nested class is a class within another class and should be accessible to members of the enclosing class (outer class). This class is also known as an inner class in Java. While an inner class exists within the outer class, you must instantiate the outer class first in order to instantiate the inner class.

Example

Output

Inner class show() method : outer_a = 75

Explanation

In the above program, we have created OuterClass and InnerClass classes, and also defined test()method in OuterClass and show() method in InnerClass. As you can see, we are able to access the outer_a variable directly without creating the object of an outer class.

That’s all about how to have multiple classes in one file in Java

Related Posts

  • Check if Object is null Java
    14 March

    Check if Object Is Null in Java

    Table of ContentsComparison Operator to Check if Object Is Null in JavaisNull() Method to Check if Object Is Null in JavanonNull() Method to Check if Object Is Null in JavarequireNonNull() Method to Check if Object Is Null in JavaConclusion An object in Java is an instance of a class. It is a real entity existing […]

  • How to print multiple variables in java
    31 January

    How to Print Multiple Variables in Java

    Table of ContentsWays to Print Multiple Variables in JavaUsing System.out.printUsing System.out.printfPrint Multiple Variables in Java Using LoggerFrequently Asked QuestionsHow Do I Print Multiple Values on One Line in JavaHow to Print Multiple Integers in JavaHow to Print String and Integer in Same Line in Java In this post, we will see how to print multiple […]

  • What is == in java
    01 December

    What is == in java

    Table of ContentsWhat is meaning of == in JavaCode example of ==Object equality using equal to ==The difference between == and equals== in EnumConclusion In this tutorial, we will learn what is == in Java as it is a common lexeme that we will use while developing applications. The == lexeme can be confused with […]

  • Break out of nested loop in java
    22 July

    How to break out of nested loops in Java

    Table of ContentsUsing break (will break inner loop)Using named loopUsing named blockUsing returnConclusion In this post, we will see how to break out of nested loops in Java. Using break (will break inner loop) It is very important to understand how nested loops work to ensure that applying break will output the desired result. If […]

  • public static void main(String args[]) - Java main method
    11 June

    public static void main(String[] args) – Java main method

    Table of ContentsWhy is the main method so important?SyntaxExplanation of the KeywordsExamples of Java main method ProgramsSimple Java ProgramPassing arguments in the main methodNon-Public main method.Defining a return value for the main methodOverloading the main methodQuestionsCan we run java without main method?Can we have more than one main method in classConclusion If you have worked […]

  • Java projects for beginners
    30 April

    Top 20 Java Projects for Beginners

    This tutorial provides the top 20 java projects for beginners for practice. These projects will help you to learn java quickly and efficiently.

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.