Reputation: 2299
I have gone through various questions about public inner classes in this forum, however I didn't see any answer for what I am trying to find, so yet again another question about Java Public Inner Classes. I am reviewing another developer's code and this person has made use of a lot of inner classes. Here's an example
public class A{
private class B
private class C
private class D
:
:
public class B{
}
public class c{
}
public class D{
}
}
So when I asked the developer about why she used inner classes, she told me, since these classes are going to be ONLY used by this class, she did it this way, which I agree is a valid reason,
However the class has become a "long" (around 200 lines of getters and setters including 4-5 class definitions) class, with bunch of inner classes. Though I agree with her reason, I am concerned about the length of the class. I almost feel like telling her to remove the inner classes and make them classes of their own, but for now, the only advantage of doing this is it reduces the size of the class.
I personally avoid using inner classes, it might just be a habit or I am just dogmatic when it comes to this, so I don't want my personal bias to dictate the review. That's why, I would like to know from you guys, what other advantages or disadvantages are there of what I am proposing? (i.e. move them to their own classes) Thanks.
Upvotes: 2
Views: 197
Reputation: 14548
I see no problem with that if the inner classes are only going to be used in the outer class and wont be used anywhere outside. Maybe formatting can be changed to make it easy to read/maintainable if not done already
Upvotes: 0
Reputation: 340723
If the only reason of having an inner class (do not be confused with static inner classes) is to reduce the visibility of B
, C
and D
, maybe collecting all classes in the same package and using package private visibility modifier (i.e. no modifier at all) would be enough?
package com.example
public class A {
}
class B {
}
class c {
}
class D {
}
Now only class A
is available throughout the whole project, while B
, C
and D
are only available within com.example
package. Please remember that using inner classes comes with a price of storing additional implicit this
reference to parent class (A
in this example).
Upvotes: 1
Reputation: 17631
The only difference I know is that inner classes can access private instance variables of the outer class. The use of private inner classes prevents access from other classes, too.
Logical grouping of classes—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.
Increased 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.
More readable, maintainable code—Nesting small classes within top-level classes places the code closer to where it is used.
Upvotes: 2