Inner Classes in Java

  • Facebook
  • Google
  • LinkedIn
  • Twitter

Inner class or nested class in Java is defined inside the body of other class. Java inner class can be declared public, protected, private or with default access whereas an outer class can have only public or default access. Inner class instance has access to all member of the outer class.

Syntax of java Inner class

class Outer
{
    private String address = " ";
    class Inner {   }         
}

Here Inner class can access the address member of the Outer class as it is declared as private.

Advantage of Inner classes in java

  • Inner class instance has access to all member of the outer class including private fields also.
  • Inner classes are good when they are not used outside the containing class.
  • Inner classes are good to develop more readable and maintainable code because classes and interfaces are in one place only so it requires less code to write.
  • Inner classes provide better packaging convenience as well.
  • If a class is used for only one other class then make this class an inner class is best idea.
  • In Inner class we can specify any of the four access specifiers like private, package, protected, or public. But in top level class we cannot specify these specifiers, which can only be declared public or package.

Here are two types of Nested classes in java.

  1. Static nested class.
  2. Non-static nested class (this type of class is called inner class)

Static nested class:

A static class created within class is called static nested class. Static nested classes can access only static members of the outer class. We can instantiate it without having to instantiate the outer class but can be accessed by outer class name.

Example of static nested class

package com.javatutsworld.nested;

class Outer {
	static String name = "Mohd. Saaim";

	static class Inner {
		void printMessage() {
			System.out.println("Hello " + name);
		}
	}

	public static void main(String args[]) {
		Outer.Inner obj = new Outer.Inner();
		obj.printMessage();
	}
}

Output


Hello Mohd. Saaim

In above example we do not need to create object of outer class but we need to create the instance of static nested class using outer class.

Non-static nested class

These are the 3 different types of non-static nested classes.

  1. Member Inner class
  2. Local Inner class
  3. Anonymous Inner Class

Member Inner class

This inner class is defined as a member of the outer class. We need to instantiate the outer class and then instantiate the inner class.

Example of Member Inner class


package com.javatutsworld.nested;

class MemberOuter {
	private String name = "Mohd. Aariz";

	class Inner {
		void printMessage() {
			System.out.println("Hello " + name);
		}
	}

	public static void main(String args[]) {
		MemberOuter obj = new MemberOuter();
		MemberOuter.Inner inner = obj.new Inner();
		inner.printMessage();
	}
}

Output


Hello Mohd. Aariz

Local Inner class

This class is defined inside a method body of outer class is called local inner class. Local inner class is not associated with Object so we cannot use private, public or protected access modifiers with this class. The modifiers used abstract or final only.

Example of Local Inner class


package com.javatutsworld.nested;

public class LocalInner {
	private String name1 = "Mohd. Saaim";
	String name2 = "Mohd. Aariz";

	void show() {
		class Local {
			void printMessage() {
				System.out.println("Hello " + name1 + " and " + name2);
			}
		}
		Local l = new Local();
		l.printMessage();
	}

	public static void main(String args[]) {
		LocalInner obj = new LocalInner();
		obj.show();
	}
}

Output


Hello Mohd. Saaim and Mohd. Aariz

Anonymous Inner class

An inner class declared without a class name is known as anonymous inner class. This class that is declared and instantiated at the same time and mostly used whenever we need to override the method of a class or an interface.

Example of Anonymous Inner class


package com.javatutsworld.nested;

abstract class AnonymousInner {
	abstract void printMessage();

	public void bye() {
		System.out.println("Bye");
	}
}

class TestAnonymousInner {
	public static void main(String args[]) {
		AnonymousInner p = new AnonymousInner() {
			void printMessage() {
				System.out.println("Hello Saaim");
			}
		};
		p.printMessage();
		p.bye();
	}
}

Output


Hello Saaim
Bye
      Advertisements