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.
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.
From oracle java tutorial: