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


import_contacts

You may also like:

Related Posts

  • 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.

  • How to end program in java
    08 March

    How to end program in java

    Table of ContentsUsing System.exit() to end java programExample to demonstrate System.exit() methodHow to end java program in an if statement in javaUsing return to end current method Ho In this post, we will see how to end program in java. You can use System.exit(0) to end program in java. Using System.exit() to end java program […]

  • 25 September

    Reference Variable in Java

    Table of ContentsReference variableStatic Reference VariableInstance Reference VariableLocal Reference Variable In this post, we will see about Reference variable in java. Reference variable A variable that holds reference of an object is called a reference variable. Variable is a name that is used to hold a value of any type during program execution. If the […]

  • 26 March

    Global variables in java

    In this post, we will see how to define global variables in java. Unlike C/C++, there are no standard global variables in java but you can still define global variables that can be used across all classes. Global variables are those variables that can be accessed across all the classes. Java does not support global […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.