Java – where to put non-public classes?
Suppose I have a Java class A, which needs an auxiliary class B. This auxiliary class is only used in a and has no other purpose In addition, B does not need to use a in any way (do not call methods or access fields)
So the question is: where to put B?
The following options are available:
>Statically nested classes In my opinion, it just makes the code less clear (more indentation, etc.)
public class A {
...
private static class B { ... }
}
>Private classes from the same source I like this option
public class A {
...
}
class B {
...
}
>Private classes in separate sources It seems that this option has a little overhead
// A.java
public class A {
...
}
// B.java
class B {
...
}
Now, I prefer the second option What do you think of it? What are best practices? Does it have an authoritative source?
Solution
I strongly favor option (1) The idea is that class B only needs class A, and option (1) is the only option to express the intention clearly: Class B is part of class A
