3

I was going through the legacy code base and observed that so many places they are using the public static class inside an outer class and nested public static class is not just being used in the outer class but its being used in so many other class?

What would be the design decision behind that and if it being used outside as well then why it wasn't created as a standalone public class in the first place itself.

So in my example it looks like below :-

public class OuterNestedClass {
    private int a;
    private List<InnerClass> innerClasses;

    public static class InnerClass {
        private int b;

        public InnerClass(int b) {
            this.b = b;
        }

        public void show(){
            System.out.println("Value of b "+b);
        }
    }
}

And other class which uses the innerclass looks like below :-

public class OutsideClass {
    public static void main(String[] args) {
        OuterNestedClass.InnerClass innerClass = new OuterNestedClass.InnerClass(10);
        innerClass.show();
    }
}

Let me know if any clarification is required.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • [This answer](https://stackoverflow.com/a/70687/5241933) may help you out - it has a list of the different types of inner classes and reasons why you might use them – David Rawson Jun 25 '17 at 05:15
  • @ElliottFrisch , sorry i didn't get you. –  Jun 25 '17 at 05:20
  • @user7851946 The fact that it is instantiated, and used externally is a sign of someone in a hurry to me. And thus it was that feature X was implemented by day Y. – Elliott Frisch Jun 25 '17 at 05:21
  • @ElliottFrisch , got it , nice excuse of bad design :) . –  Jun 25 '17 at 05:24
  • There are no inner classes here, so [tag:inner-classes] is irrelevant, and your class should not be called `InnerClass`. `OuterNested` is a contradiction in terms. – user207421 Jun 25 '17 at 06:29
  • 1
    There may be hundreds or thousands of reasons why people do it. A lot of them may be bad reasons. But your question as it stands is way overbroad. If you could focus it down to a particular use of static public member classes, then perhaps people can provide their idea why the code author did it - but unless the code author is here on StackOverflow to answer your question, it will still be a very much opinion-based answer. (note I know you posted an example, but this is not a real-world case - it looks like you wrote this yourself? in that case, you should know why) – Erwin Bolwidt Jun 25 '17 at 06:42
  • @ErwinBolwidt , i can't share the exact code due to copyright issues but the example which i share is similar to what is there in the code base. –  Jun 25 '17 at 07:16

1 Answers1

3

Main reason for that would be namespacing. Static classes are utility classes, and those tend to grow very large. Nested classes let you break your utilities nicely, while still keeping everything together.

So, instead of having Utils with 20 methods, you probably have Utils.Password with 5 and Utils.Json with 15

Best example for that I've seen is how Retrofit.Builder is done: https://github.com/square/retrofit/blob/master/retrofit/src/main/java/retrofit2/Retrofit.java#L394

Alexey Soshin
  • 16,718
  • 2
  • 31
  • 40
  • hmm, make sense, do you have any javadoc link or some other post describing this in details ? –  Jun 25 '17 at 05:22
  • In a utility class the constructor is typically `private` so no one instantiates it. OP's example just seems like bad design. – Elliott Frisch Jun 25 '17 at 05:28
  • Added a good example. Although I tend to agree with @ElliottFrisch, that probably it was done without much thought. – Alexey Soshin Jun 25 '17 at 05:37