1

Is it bad practice or "dumb" to access methods/fields that are private in the public class from private classes in the same file. In my case I have a method that add components in my GUI to panels(GridBagLayout) so I have made a method for this. However I have three panels so instead of making a addComponent-method in each private class I have the private method addComponent in the public class.

This is a overview of my class:

  • RegisterQuestionGUI (public)
    • This class has many methods, one of them is a private method named addComponent.
    • I also have three private classes that extend JPanel, and all of these classes use the addComponent in exactly the same way.

So back to my question, is this a good/bad way of doing it?

Thanks in advance.

6 Answers 6

4

A private inner class is part of the public "outer" class. Therefore, accessing the private members of the outer class is perfectly acceptable.

Sign up to request clarification or add additional context in comments.

Comments

3

In general, I don't see an issue with it. Private inner classes are part of the implementation of the outer class, so encapsulation is not broken. OTOH getting rid of duplication is a good thing.

AFAIK this idiom is used many times in the class library (it is there for a reason, after all :-), e.g. when implementing Iterators in the Collection Framework. Its typical usage tends to have the following common traits:

  • you need to implement a specific interface without publishing the concrete implementation class, however
  • the implementation is tightly bound to some public class (making the two in fact a component).

Implementing the interface in a private inner class nicely satisfies both constraints at once, making the logical codependency of the two classes explicit, and encapsulating the implementation class.

Comments

2

It is excellent.

You need private classes because (I guess) you have to implement certain interfaces (i.e. EventListener etc). You make them inner classes because they are irrelevant beyond outer class. But you re-use the code creating private utility in outer class. So, you are a good programmer.

Comments

2

It depends.

If the private classes are trivial helpers, it can be reasonable to think of them as part of the implementation of the main class.

However, you might want the private class to access only non-private methods of its containing class if:

  • It is a non-trivial nested class.
  • If you might someday want to move the nested class, for example, to become a top-level class.

Comments

1

I'd say that was perfectly acceptable - I've done similar things in the past. Accessing private variables from an inner class is allowed for a reason, in many situations (not just this one) it makes sense to do so.

3 Comments

Just a note: if I remember correctly, technically you can't access private members of the enclosing class from an inner class, but the compiler generates synthetic accessor methods for you.
Yeah, that's probably have to with the encapsulation of the class.
It's allowed because part of the whole point of inner classes is that they're "part" of their enclosing class; it therefore makes complete sense for the inner class to have access to the outer class' details. It allows code to be neater if you can accomplish something in an inner class rather than using a separate class and exposing parts of the object you'd otherwise want to encapsulate.
1

In general this is fine. However, depending on what these JPanel classes are, it might make more sense to break them out a separate classes. Maybe even have them implement the same interface so that your RegisterQuestionGUI can interact with them the same way.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.