![]() ![]() This is extra work, so it may be more convenient to declare the member public in the first place. It is possible to make it operate on private members by doing a few things like adding a public getter. For example, the Jackson JSON serializer by default only operates on public members. ![]() Sometimes inner classes are used in combination with reflection-based frameworks that only operate on public members.Of course, at that point, it might warrant refactoring the inner class to its own top-level class. Although it wouldn't be enforced by the compiler, marking an inner class's members as private can document for future maintainers that those members are not intended to be accessed directly by the outer class code.It's preferrable to keep implementation details private in this case. In this case, the inner class is subject to the same best practices for member visibility as top-level classes. Perhaps the outer class has a method that returns an instance of the inner class. Sometimes inner classes are declared public and serve as part of the interface definition of the containing class. ![]() Here are a few additional considerations though: As you pointed out, the containing class can access all members anyway. At first glance, it seems irrelevant to specify an access modifier on the members of inner classes.
0 Comments
Leave a Reply. |