In Java programming language you can define a class within another class. Such a class is known as a nested class in Java.
Nested class categories
Java Nested classes are divided into two categories-
- Static nested classes- Nested classes that are declared static are called static nested classes.
General form of static nested class in Java-
class OuterClass { ... static class StaticNestedClass { ... } }
- Non-static nested classes- Nested classes that are not static are called Non-static nested classes. They are
known better as inner classes in Java.
General form of inner class in Java-
class OuterClass { ... class InnerClass { ... } }
Java Static nested class example
public class NestedDemo { int num = 10; public static class Nested{ NestedDemo nd = new NestedDemo(); private int quantity = 10; void msg(){ System.out.println("data is "+ nd.num * quantity); } } public static void main(String[] args) { NestedDemo.Nested nested = new NestedDemo.Nested(); nested.msg(); } }
Output
data is 100
Benefits of using Nested Classes in Java
- Nested classes provide a way to logically group classes that are only used in one place.
If a class is useful to only one other class, then it makes sense to embed it in that class and keep the two together. Nesting such "helper classes" makes their package more streamlined. - It increases encapsulation. Note that a nested class has access to all the members of the outer class even private members. Consider two top-level classes, A and B, where B needs access to members of A that would otherwise be declared private. By hiding class B within class A, A's members can be declared private and B can access them. In addition, B itself can be hidden from the outside world.
- It can lead to more readable and maintainable code: Nesting small classes within top-level classes places the code closer to where it is used.
Important points about static nested class
- Just like
static class methods, a static nested class cannot refer directly to instance variables or methods defined
in its enclosing class, it can use them only through an object reference.
- Refer post static reference to the non-static method or field error to know why non-static method or field can't be called from a static method.
- Static nested classes are accessed using the enclosing class name: NestedDemo.Nested
- Since nested class is a member of its enclosing class, a nested class can have any of the access modifiers; private, public, protected, or package private. Note that outer classes can only be declared public or package private.
Inner class in Java
An inner class is a nested class that is not static. Inner class has access to all the members (fields and methods) of the outer class including those with access modifier as private. Let’s see a simple inner class example here to make it clearer.
Java Inner class example
In this example there is a OuterClass with one private field, a method createInner() and an inner class InnerClass. Inner class has one method displayMsg() which takes String as argument.
class OuterClass { private int outerVar = 12; // Inner class class InnerClass{ void displayMsg(String msg){ System.out.println("Message is - " + msg); System.out.println("Outer class variable - " + outerVar); } } void createInner(){ InnerClass ic = new InnerClass(); ic.displayMsg("Calling From a method with in the Outer class"); } } public class ICDemo { public static void main(String[] args) { OuterClass oc = new OuterClass(); oc.createInner(); // Creating inner class object using outer class object OuterClass.InnerClass oic = oc.new InnerClass(); oic.displayMsg("Calling from outside the OuterClass scope"); } }
Output
Message is - Calling From a method with in the Outer class Outer class variable - 12 Message is - Calling from outside the OuterClass scope Outer class variable - 12
Points to note
There are several points of interest to note here about the inner classes–
- From the output you can see that inner class has access to fields and methods of the outer class, even if they are private. In the displayMsg() method of the inner class outerVar variable is accessed which is private and resides with in the scope of outer class.
- If you want to make an object of the inner class with in a non-static method of the outer class you don’t need to qualify it with Outer class name. Since createInner() method is a method of the outer class and it is not static you can create an object of the inner class directly.
- If you want to make an object of the inner class outside the scope of the outer class or from a method with in the outer class which is static you have to qualify inner class with the outer class so the type of the object would be specified as OuterClassName.InnerClassName. You can see example of object creation of inner class that way in the main method as it resides in another class ICDemo.
- An inner class can be private, public, protected or package-private. Note that normal class can only be public or package-private (default).
Types of inner classes
Apart from non-static nested class (also known as member inner class) there are two additional types of inner classes in Java.
- Local inner class
- Anonymous inner class
Local inner class in Java
Local inner classes are inner classes that can be defined within a block, here block means any group of zero or more statements between balanced braces. Generally you will find local inner classes defined within a method body.
Java Local inner class example
Let’s see an example of local inner class where inner class is defined in the method msg. Here Local inner class is also implementing an interface Message.
package org.prgm; // Interface interface Message{ String readMsg(); } class Outer { private int outerVar = 12; public Message msg(String str){ // Local variable - it has to be final or // effectively final String hello = "Hello "; // Local Inner class class InnerMsg implements Message{ private String msg; InnerMsg(String msg){ this.msg = msg; } @Override // implementing interface method public String readMsg() { //hello = "Hi"; System.out.println("I can read outer class variable its value is - " + outerVar ); return hello + msg; } } return new InnerMsg(str); } } public class ICLocalDemo { public static void main(String[] args) { Outer outer = new Outer(); Message message = outer.msg("Local Inner Class"); String temp = message.readMsg(); System.out.println("temp -- " + temp); } }
Output
I can read outer class variable its value is - 12 temp -- Hello Local Inner Class
Points to note
There are several points of interest in the above code so let’s go through those points and try to get a better explanation.
- Here inner class InnerMsg is defined with in the method msg that is why it is local inner class as it is local to the method msg.
- Local inner class can access members of the enclosing class, you can see that int variable outerVar is accessed with in the inner class InnnerMsg.
- A local class has access to local variables (variables defined inside the method body). However, a local class can only access local variables that are declared final (before Java 8).
- Starting in Java SE 8, local class can access local variables and parameters of the enclosing block that are final or effectively final. A variable or parameter whose value is never changed after it is initialized is effectively final. In the method readMsg() if you uncomment the line- //hello = "Hi"; You will get the error “Local variable hello defined in an enclosing scope must be final or effectively final” if you are running this code in Java 8. If you are using version lesser than 8 then it will ask you to declare hello as final.
- In the above code one thing to note is that inner class implements an interface. Doing that you are abiding to a very important point of object oriented programming – Abstraction. If you have noticed the return type of the method msg is interface Message. That way your implementation of the inner class is completely hidden.
Anonymous inner class in Java
Anonymous inner class is a type of local inner class that has no name. You will always declare anonymous inner class as subclass or as implementation of an interface. Note that you cannot declare constructors in an anonymous class.
Java Anonymous inner class example
Let’s implement the same example as above as an anonymous inner class. There is an interface Message and that is implemented as an anonymous inner class with in the method msg.
//Interface interface Message{ String readMsg(); } class MessageOuter { private int outerVar = 12; public Message msg(String str){ // Local variable - it has to be final or // effectively final String hello = "Hello "; return new Message(){ @Override public String readMsg() { //hello = "Hi"; System.out.println("I can read outer class variable its value is - " + outerVar ); return hello + str; } }; // required to have semicolon } } /** * */ public class ICAnonymousDemo { public static void main(String[] args) { MessageOuter mo = new MessageOuter(); Message msg = mo.msg("Anonymous Inner Class"); System.out.println("Message is - " + msg.readMsg()); } }
Output
I can read outer class variable its value is - 12 Message is - Hello Anonymous Inner Class
Points to note
There are several points of interest in the above code so let’s go through those points and try to get a better explanation.
- Here inner class with in the method msg is an anonymous inner class. It contains a new operator, name of the interface it is implementing (it can also extend a class), and class declaration body with in the braces.
- Because an anonymous class definition is an expression, it must be part of a statement. That is why there is a semicolon after the closing brace.
- Anonymous inner class can access members of the enclosing class, you can see that int variable outerVar is accessed with in the method of the anonymous inner class.
- Anonymous inner class has access to local variables (variables defined inside the method body). Same rules apply as in the case of local inner class.
- You cannot declare constructors in an anonymous class as it has no name.
That's all for this topic Java Nested Class And Inner Class. If you have any doubt or any suggestions to make please drop a comment. Thanks!
>>>Return to Java Basics Tutorial Page
Related Topics
You may also like-