Signed in as:
filler@godaddy.com
Signed in as:
filler@godaddy.com
Java inner class or nested class is a class that is declared inside the class or interface.
We use inner classes to logically group classes and interfaces in one place to be more readable and maintainable.
Additionally, it can access all the members of the outer class, including private data members and methods.
There are three advantages of inner classes in Java. They are as follows:
Sometimes users need to program a class in such a way so that no other class can access it. Therefore, it would be better if you include it within other classes.
If all the class objects are a part of the outer object then it is easier to nest that class inside the outer class. That way all the outer class can access all the objects of the inner class.
Do You Know
An inner class is a part of a nested class. Non-static nested classes are known as inner classes.
There are two types of nested classes non-static and static nested classes. The non-static nested classes are also known as inner classes.
Member Inner Class : A class created within class and outside method.
Anonymous Inner Class : A class created for implementing an interface or extending class.
The java compiler decides its name.
Local Inner Class : A class was created within the method.
Static Nested Class : A static class was created within the class.
Nested Interface : An interface created within class or interface.
==========================================================================
A non-static class that is created inside a class but outside a method is called member inner class. It is also known as a regular inner class. It can be declared with access modifiers like public, default, private, and protected.
Syntax:
In this example, we are creating a msg() method in the member inner class that is accessing the private data member of the outer class.
TestMemberOuter1.java
Output:
data is 30
An object or instance of a member's inner class always exists within an object of its outer class. The new operator is used to create the object of member inner class with slightly different syntax.
The general form of syntax to create an object of the member inner class is as follows:
Syntax:
Example:
Here, OuterClassReference is the reference of the outer class followed by a dot which is followed by the new operator.
The java compiler creates two class files in the case of the inner class. The class file name of the inner class is "Outer$Inner". If you want to instantiate the inner class, you must have to create the instance of the outer class. In such a case, an instance of inner class is created inside the instance of the outer class.
The Java compiler creates a class file named Outer$Inner in this case. The Member inner class has the reference of Outer class that is why it can access all the data members of Outer class including private.
==========================================================================
Java anonymous inner class is an inner class without a name and for which only a single object is created. An anonymous inner class can be useful when making an instance of an object with certain "extras" such as overloading methods of a class or interface, without having to actually subclass a class.
In simple words, a class that has no name is known as an anonymous inner class in Java. It should be used if you have to override a method of class or interface. Java Anonymous inner class can be created in two ways:
TestAnonymousInner.java
Output:
nice fruits
Output:
nice fruits
It performs two main tasks behind this code:
============================================================================
A class i.e., created inside a method, is called local inner class in java. Local Inner Classes are the inner classes that are defined inside a block. Generally, this block is a method body. Sometimes this block can be a for loop, or an if clause. Local Inner classes are not a member of any enclosing classes. They belong to the block they are defined within, due to which local inner classes cannot have any access modifiers associated with them. However, they can be marked as final or abstract. These classes have access to the fields of the class enclosing it.
If you want to invoke the methods of the local inner class, you must instantiate this class inside the method.
LocalInner1.java
Output:
30
In such a case, the compiler creates a class named Simple$1Local that has the reference of the outer class.
Local variables can't be private, public, or protected.
Rules for Java Local Inner class
1) Local inner class cannot be invoked from outside the method.
2) Local inner class cannot access non-final local variable till JDK 1.7. Since JDK 1.8, it is possible to access the non-final local variable in the local inner class.
LocalInner2.java
=========================================================================
A static class is a class that is created inside a class, is called a static nested class in Java. It cannot access non-static data members and methods. It can be accessed by outer class name.
TestOuter1.java
Output:
data is 30
In this example, you need to create the instance of static nested class because it has instance method msg(). But you don't need to create the object of the Outer class because the nested class is static and static properties, methods, or classes can be accessed without an object.
If you have the static member inside the static nested class, you don't need to create an instance of the static nested class.
TestOuter2.java
Output:
data is 30
==============================================================================
An interface, i.e., declared within another interface or class, is known as a nested interface. The nested interfaces are used to group related interfaces so that they can be easy to maintain. The nested interface must be referred to by the outer interface or class. It can't be accessed directly.
There are given some points that should be remembered by the java programmer.
In this example, we will learn how to declare the nested interface and how we can access it.
TestNestedInterface1.java
Output:
hello nested interface
download the example of nested interface
As you can see in the above example, we are accessing the Message interface by its outer interface Showable because it cannot be accessed directly. It is just like the almirah inside the room; we cannot access the almirah directly because we must enter the room first. In the collection framework, the sun microsystem has provided a nested interface Entry. Entry is the subinterface of Map, i.e., accessed by Map.Entry.
The java compiler internally creates a public and static interface as displayed below:
Let's see how we can define an interface inside the class and how we can access it.
TestNestedInterface2.java
Output:
hello nested interface
Yes, if we define a class inside the interface, the Java compiler creates a static nested class. Let's see how can we define a class within the interface:
================================================================================
We use cookies to analyze website traffic and optimize your website experience. By accepting our use of cookies, your data will be aggregated with all other user data.