In this post, we will see how to have multiple classes in one file in java.
Table of Contents
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
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
class Organization { private int totalProjects; Organization() { System.out.println("Inside Organization class constructor"); this.totalProjects = 0; System.out.println("Total Organization projects: " + this.totalProjects); } public void createProject() { Project proj1 = new Project(10); // creating a new project object instance proj1.displayDuration(); Project proj2 = new Project(20); // creating another project object instance proj2.displayDuration(); } private void greetings() { System.out.println("Greetings from private method of Organization class"); } class Project { private int durationInDays; public int dummyVariable = 10; Project(int duration) { System.out.println("Inside Project class constructor."); this.durationInDays = duration; totalProjects++; System.out.println("Total Company projects: " + totalProjects); System.out.println("Calling private method of Organization class from Project class"); greetings(); } public void displayDuration() { System.out.println("Inside Project class displayDuration method"); System.out.println("Project duration: " + this.durationInDays); } } } class NestedClassTest { public static void main(String[] args) { Organization orgObject = new Organization(); // create an object // now create a new project of the Organization orgObject.createProject(); } } |
output
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
class OuterClass { int outer_a = 75; void test() { InnerClass inner = new InnerClass(); inner.show(); } // Inner Class class InnerClass { void show() { System.out.println("Inner class show() method : outer_a = " + outer_a); } } } class InnerClassTest { public static void main(String args[]){ OuterClass outer = new OuterClass(); outer.test(); } } |
Output
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