Have you ever heard something like nested classes, inner classes, anonymous classes…? This tutorial gives you the concepts of nested classes in the Java programming language along with code examples. And we focus on explaining the concepts of static nested classes and inner classes.

By understanding how nested classes work, you will be able to write code more flexibly and more succinct in order to take the fullest extent of the Java programming language.

 

1. What is a Nested Class in Java?

A class is declared within another class is called a nested class. Here’s an example:

public class Car {

	class Engine {
	
	}
}
Here, the class Engineis nested within the class Car. Engine is the nested class and Car is the outer class (or enclosing class).

 

2. Why We Use Nested Classes in Java?

Here are the main reasons that motivate us to use nested classes:

- Grouping related code together: some classes are only useful to others, e.g. the Engine class and Car class in the above example. By using nested classes, related code is grouped together which gives us more flexibility and controllability in writing code.

- Increasing code encapsulation: consider the above example - the nested class Engine is encapsulated inside the Car class, which protects the Engine class from the outside world.



- Making the code more readable and maintainable: Indeed, nested classes help us write more readable and maintainable code, as shown in the above example: when reading to the Car class, we can also navigate to the Engine class and make update quickly within the same source file.

Do you know there are two types of nested classes? They are static nested ones and inner ones. Let’s look at each now.

 

3. What is a Static Nested Class in Java?

A nested class is marked with static modifier is called the static nested class. Here’s an example:

public class Car {

	static class Wheel {

		public void rotate() {
			System.out.println("The wheel is rotating...");
		}
	}	
}
Here, Wheel is a static nested class which is enclosed in the Car class. The following code illustrates how to instantiate an object of the static nested class:

Car.Wheel wheel = new Car.Wheel();
wheel.rotate();
Output:

The wheel is rotating…
As we can see, a static nested class does not have any relationship with the outer class, except for the purpose of packaging and encapsulation.

Like static methods, the static class can have only direct access to the static members of the enclosing class. For example:

public class Car {
	private int weight;
	private static String brand;

	static class Wheel {

		public void rotate() {
			// cannot access instance variables of the enclosing class
			//weight = 5;

			// can access static members of the enclosing class
			brand = "Honda";


		}

	}

}
As we can see, the static nested class cannot have direct access to instance members of the enclosing class, except via object reference of the enclosing class. Here’s an example:

public class Car {
	private int weight;

	static class Wheel {
		private static int spokes;
		private String name;

		public void test(Car car) {
			car.weight = 5000;
			System.out.println("car's weight is: " + car.weight);
		}
	}
}
 

Testing code:

Car car = new Car();
Car.Wheel wheel = new Car.Wheel();
wheel.test(car);
Output:

car's weight is: 5000
An interesting point is that the static nested class is just like a member of the enclosing class. That means we can use access modifiers for the static nested class, i.e. public, protected, private and package private.

A static class can also have instance and static members just like a regular class.

 

4. Nested Interfaces in Java:

It’s also possible to have an interface nested within a class. For example:

public class Car {

	interface Control {
		void start();
		void stop();
		void brake();
	}
}
Interfaces are static by default, thus we can instantiate an object of an implementing class like this:

Car.Control controller = new CarControl();
controller.start();
 

Provided that CarControl is the following class:

class CarControl implements Car.Control {

		public void start() { }

		public void stop() { }

		public 	void brake() { }
}
That’s interesting, right?

 

5. What is an Inner Class in Java?

An inner class is a nested class which is not static. In other words, an inner class is like an instance member of the enclosing class. Here’s an example:

public class Computer {

	class Screen {

	}
}
An inner class is associated with an instance of the enclosing class, thus we have to create a new object of the inner class like this:

Computer comp = new Computer();
Computer.Screen screen = comp.new Screen();
 

As we can see, unlike a static nested class, we need an object reference of the enclosing class in order to instantiate the inner class. Note that the place of the new key word used here:

Computer.Screen screen = comp.new Screen();
The inner class has direct access to all instance members of the enclosing class, even they are declared as private.

But an inner class cannot have static members (though it can have direct access to static members of the enclosing class).

 

Other Recommended Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.



Add comment

   


Comments 

#1Abhijeet Raut2022-07-30 01:17
Code is :
Car.Control controller = new CarControl();
Code I think should be :
Car.Control controller = new Car.Control();
--Below is the page link where you can find that code.
Page ->codejava.net/.../...
Quote