I have class A with its inner class defined A1 and class B with its inner class defined B1. Do you think it is alright that class A in its implementation refers to B1 and class B refers to A1. Is it not a bad programming style? Its just A1 is very A specific class and B1 very B specific, that's why I coupled them. Is it Ok to leave it like that or its better to have A1 and B1 as separate classes? What do you think? Thx.
-
Going only on the details you've provided, I don't see how it can make a difference either way. – Anonymoose May 20 '11 at 13:32
5 Answers
You could - though I would think the point of inner classes is because they should be used within the outer classes. In other words, I am not a big fan of public inner classes.
I would actually just put A1 and B1 out as its own class - seem to me A1 and B1 are not that specific to A and B after all since A1 is used by B and B1 is used by A.
Look at this post Why/when should you use nested classes "Use a nested class when the class you are nesting is only useful to the enclosing class"
-
Yes, but the post you refer to is for .NET... I don't know about all .NET, but at least in C#, nested classes are a not nearly as powerful as Java's inner classes. For example, a C# nested class does not have access to the outer-classes non-static methods without an explicit reference to an instance. So I don't think this link is directly translatable. – Dilum Ranatunga May 20 '11 at 13:49
-
Yeah, but it still does give general conceptual view that applies across OO-concept – TS- May 20 '11 at 14:05
-
See my answer below... IMO nested classes are *mostly* used to exploit/utilize language specifics. – Dilum Ranatunga May 20 '11 at 14:09
I believe that if you need to refer to inner classes of another class, in one of your classes, it is because possibly the class which holds the inner class should be providing some methods to avoid this problem.
If the problem is not like that, then the inner class is probably generic enough to be of use in both other classes, and as such should be an independant class, which should be probably part of the same package which specifies its context of application/use.

- 5,029
- 2
- 41
- 41
-
@Luis but the basic premise of using an inner class is that it wont be needed by any other class, then how come it is a good practice ? correct me if am wrong. – Subham Tripathi Aug 11 '14 at 04:25
-
That is what I said. An inner class should only be used by its parent class. And it is a good practice to use it in that context because it provides better encapsulation and organization of your code and the concepts which you are representing. If you need to use that code elsewhere, then it's not meant to be an inner class. – Luis Miguel Serrano Aug 11 '14 at 15:51
I would possibly try to two things:
Move inner classes to the separate ones, as they are not inner any more in logical way.
Create some interfaces to make A and B not to know at least where the A1 and B1 classes are.

- 42,730
- 18
- 77
- 103
I'd say if both classes are in the same package, extract the inner classes and make them package private. That way, both classes (A and B) can refer to them but still they are invisible to other classes outside the package.

- 59,493
- 71
- 188
- 276
I think you've asked two separate design questions in one. The first question is about referring to another class's inner classes. The second question is about the circular dependency between A/A1 and B/B1.
Exposed Inner Classes
There are many reasons to use Java inner classes.
- Group classes that are only relevant to one-another and the outer class. (a.k.a mini-package)
- Reduce boilerplate code in passing and referring to outer class instances
- Add an additional level of scoping beyond package
- Expanding the semantics of Java's access scope... for example,
private
has a different meaning for outer-class -- inner-class interactions. - Reduce the explosion of source files... especially in (source) code generation scenarios.
- Access to outer class internals for testing (the inner class can be removed during packaging of JAR).
Under many of these circumstances, referring to the inner class by name is perfectly reasonable.
Circular Dependency
This is more often a tell tale sign of needing to extract interfaces, or needing extract other specialized classes. If A1, B1 are too simple to extract interfaces etc, then perhaps you can use inner classes in a different way: Have outer class C, containing A1, B1.

- 13,254
- 3
- 41
- 52