English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In Java, like methods, a class variable can also take another class as its member. Java allows you to write a class inside another. The class written inside is callednested class, and the class that saves the inner class is calledouter class.
The following is the syntax for writing nested classes. Here,Outer_DemoThe class is an outer class, whileInner_DemoThe class is a nested class.
class Outer_Demo { class Inner_Demo { } }
Nested classes are divided into two types.
non-static nested classes-These areclassnon-static members.
static nested classes-These areclassof static members.
An inner class is a security mechanism in Java. We know that a class cannot be accessed by an access modifierprivateAssociation, but if we take this class as a member of another class, we can set the inner class as private. This is also used to access the private members of a class.
Inner classes are divided into three types, depending on how and where you define them. They are-
Inner level
Method Local Inner Class
Anonymous Inner Class
Creating an inner class is very simple. You just need to write a class within a class. Unlike classes, inner classes can be private, and once the inner class is declared as private, it cannot be accessed from an object outside the class.
The following program demonstrates how to create an inner class and access it. In the given example, we set the inner class to private and access the class through a method.
class Outer_Demo { int num; //Inner class private class Inner_Demo { public void print() { System.out.println("This is an inner class"); } } // Accessing the inner class from the method within void display_Inner() { Inner_Demo inner = new Inner_Demo(); inner.print(); } } public class My_class { public static void main(String args[]) { //Instantiate the external class Outer_Demo outer = new Outer_Demo(); //Accessing the display_Inner() method. outer.display_Inner(); } }
Here, you can observeOuter_Demois the external class,Inner_Demois the inner class,display_Inner()is the method in which the inner class is instantiated, and this method is frommainmethod call.
If you compile and run the above program, you will get the following results.
Output Result
This is an inner class.
As mentioned before, inner classes are also used to access the private members of a class. Assume that a class has private members and you can access them. Write an inner class in it, and from the method within the inner class(such as getValue())Returns the private member and finally returns from another class (from which you want to access the private member)getValue()
Method of the inner class.
To instantiate an inner class, you must first instantiate the external class. After that, use the object of the external class, and here is the method to instantiate the inner class.
Outer_Demo outer = new Outer_Demo(); Outer_Demo.Inner_Demo inner = outer.new Inner_Demo();
The following program shows how to access the private members of a class using an inner class.
Example
class Outer_Demo { //The private variable of the external class private int num = 175; //Inner class public class Inner_Demo { public int getNum() { System.out.println("This is the getnum method of the inner class"); return num; } } } public class My_class2 { public static void main(String args[]) { //Instantiate the external class Outer_Demo outer = new Outer_Demo(); // Instantiating the inner class Outer_Demo.Inner_Demo inner = outer.new Inner_Demo(); System.out.println(inner.getNum()); } }
If you compile and run the above program, you will get the following results.
Output Result
This is the getnum method of the inner class: 175
In Java, we can write a class inside a method, and this will be a local type. Like local variables, the scope of an inner class is also limited to the method.
Method local inner classes can only be instantiated within the method that defines them. The following program demonstrates how to use a method local inner class.
Example
public class Outerclass { //Instance method of the outer class void my_Method() { int num = 23; // method-local inner class class MethodInner_Demo { public void print() { System.out.println("This is a method inner class ")+num); } } // end of inner class // Accessing the inner class MethodInner_Demo inner = new MethodInner_Demo(); inner.print(); } public static void main(String args[]) { Outerclass outer = new Outerclass(); outer.my_Method(); } }
If you compile and run the above program, you will get the following results.
Output Result
This is a method inner class 23
An inner class without a class name is calledAnonymous Inner Class. In the case of anonymous inner classes, we declare and instantiate them at the same time. They are usually used when we need to override methods of a class or interface. The syntax of anonymous inner classes is as follows.
Syntax
AnonymousInner an_inner = new AnonymousInner() { public void my_method() { ........ ........ } };
The following program demonstrates how to override methods of a class using an anonymous inner class.
abstract class AnonymousInner { public abstract void mymethod(); } public class Outer_class { public static void main(String args[]) { AnonymousInner inner = new AnonymousInner() { public void mymethod() {}} System.out.println("This is an example of anonymous inner class"); } }; inner.mymethod(); } }
If you compile and run the above program, you will get the following results.
Output Result
This is an example of anonymous inner class
In the same way, you can use an anonymous inner class to override methods of a concrete class as well as an interface.
Generally, if a method accepts an object of an interface, abstract class, or a concrete class, we can implement the interface, extend the abstract class, and pass the object to the method. If it is a class, we can pass it directly to the method.
However, in all three cases, you can pass an anonymous inner class to this method. This is the syntax for passing an anonymous inner class as a method argument.
obj.my_Method(new My_Class() { public void Do() { ..... ..... } });
The following program demonstrates how to pass an anonymous inner class as a method argument.
// interface interface Message { String greet(); } public class My_class { //Object Method public void displayMessage(Message m) { System.out.println(m.greet()) + ", This is an example of anonymous inner class as an argument"); } public static void main(String args[]) { //Class Instantiation My_class obj = new My_class(); // Passing an anonymous inner class as an argument obj.displayMessage(new Message() { public String greet() { return "Hello"; } }); } }
If you compile and run the above program, it will provide you with the following results.
Output Result
Hello, This is an example of anonymous inner class as an argument