Java
In this tutorial we will learn about nested classes in Java programming language.
We have already discussed about how to create a class and how to instantiate an object of a class in the previous tutorials. Feel free to check them out.
If we define a class inside another class then such classes are called nested classes.
class OuterClassName {
// some code...
class InnerClassName {
//some code...
}
}
Where, OuterClassName
is the name of the outer class which encloses the InnerClassName
class.
Following are the points to note about nested classes.
class A {
class B {
// some code...
}
}
In the following example we have an outer class Company
and an inner class Project
.
class Company {
private int totalProject;
Company() {
System.out.println("Inside Company class constructor.");
this.totalProject = 0;
System.out.println("Total Company projects: " + this.totalProject);
}
public void createProject() {
// creating a new project object
Project prj1 = new Project(10);
prj1.displayDuration();
// creating another project object
Project prj2 = new Project(20);
prj2.displayDuration();
// the following lines will give error
// because outer class can't access the inner class
// members directly
// so, commented out
//
// System.out.println("DummyVariable of Project class : " + dummyVariable);
}
private void greetings() {
System.out.println("Greetings from private method of Company class.");
}
class Project {
private int durationInDays;
// don't try to access this directly from
// outer class Company as it is a part of
// the inner class Project
public int dummyVariable = 10;
Project(int duration) {
System.out.println("Inside Project class constructor.");
// set the project duration
this.durationInDays = duration;
// increment the total projects
// note! totalProject is a private member of Company class
// but it is accessible from the inner class Project
totalProject++;
System.out.println("Total Company projects: " + totalProject);
// calling the private method of the outer class Company
// from the inner class Project
System.out.println("Calling private method of Company class from Project class.");
greetings();
}
public void displayDuration() {
System.out.println("Inside Project class displayDuration method.");
System.out.println("Project duration: " + this.durationInDays);
}
}
}
public class Example {
public static void main(String[] args) {
// create an object
Company obj = new Company();
// now create a new project of the company
obj.createProject();
}
}
Output:
$ javac Example.java
$ java Example
Inside Company class constructor.
Total Company projects: 0
Inside Project class constructor.
Total Company projects: 1
Calling private method of Company class from Project class.
Greetings from private method of Company class.
Inside Project class displayDuration method.
Project duration: 10
Inside Project class constructor.
Total Company projects: 2
Calling private method of Company class from Project class.
Greetings from private method of Company class.
Inside Project class displayDuration method.
Project duration: 20
In the above code we are creating an outer class Company
and inside it we are creating an inner class Project
.
From the output we can easily tell that we can directly access the methods and variables of the outer class Company from the inner class Project.
ADVERTISEMENT