You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Java inner class or nested class is a class which is declared inside the class or interface.
4
+
5
+
We use inner classes to logically group classes and interfaces in one place so that it can be more readable and maintainable.
6
+
7
+
Additionally, it can access all the members of outer class including private data members and methods.
8
+
9
+
##### Syntax of Inner class
10
+
11
+
```java
12
+
classJava_Outer_class{
13
+
//code
14
+
classJava_Inner_class{
15
+
//code
16
+
}
17
+
}
18
+
```
19
+
20
+
##### Advantage of java inner classes
21
+
22
+
There are basically three advantages of inner classes in java. They are as follows:
23
+
24
+
1) Nested classes represent a special type of relationship that is it can access all the members (data members and methods) of outer class including private.
25
+
26
+
2) Nested classes are used to develop more readable and maintainable code because it logically group classes and interfaces in one place only.
27
+
28
+
3) Code Optimization: It requires less code to write.
29
+
30
+
-------------
31
+
32
+
#####Difference between nested class and inner class in Java
33
+
34
+
Inner class is a part of nested class. Non-static nested classes are known as inner classes.
35
+
Types of Nested classes
36
+
37
+
There are two types of nested classes non-static and static nested classes.The non-static nested classes are also known as inner classes.
38
+
39
+
1- Non-static nested class (inner class)
40
+
41
+
- Member inner class
42
+
- Anonymous inner class
43
+
- Local inner class
44
+
2- Static nested class
45
+
46
+
47
+
----------
48
+
49
+
|Type| Description|
50
+
|-----|-------|
51
+
|Member Inner Class |A class created within class and outside method.|
52
+
|Anonymous Inner Class| A class created for implementing interface or extending class. Its name is decided by the java compiler.|
53
+
|Local Inner Class |A class created within method.|
54
+
|Static Nested Class| A static class created within class.|
55
+
|Nested Interface |An interface created within class or interface.|
0 commit comments