What are inner classes? When they should be used? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 6

What are inner classes? When they should be used?

10th Nov 2016, 10:10 AM
Jaydeep Khatri
Jaydeep Khatri - avatar
5 Answers
+ 6
declared inside the class or interface. We use inner classes to logically group classes and interfaces in one place so that it can be more readable and maintainable. it can access all outer class member s
10th Nov 2016, 5:57 PM
Boorsu Gangadharam
Boorsu Gangadharam - avatar
+ 2
The Java programming language allows you to define a class within another class. Such a class is called a nested class. Why Use Nested Classes? Compelling reasons for using nested classes include the following: It is a way of logically grouping classes that are only used in one place: If a class is useful to only one other class, then it is logical to embed it in that class and keep the two together. Nesting such "helper classes" makes their package more streamlined. It increases encapsulation: 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.
10th Nov 2016, 1:14 PM
K. T.
K. T. - avatar
+ 2
I believe inner classes are sometimes used in Java as listeners for events. Since listeners are (in my novice opinion) generally specialized to perform in a strict way, they don't need to be available outside of the outer class. Inner classes are solely intended to be used to support the outer class.
23rd Nov 2016, 9:21 AM
Andrew Michel
Andrew Michel - avatar
+ 2
all coments are good... thanks buddy
1st Feb 2017, 8:19 PM
Towhid Hasnein
Towhid Hasnein - avatar
0
Can't rearrange inner class within a class
16th Sep 2020, 12:15 PM
Abdulai
Abdulai - avatar