Why have public static class inside a class
Asked Answered
S

6

16

I was going through some code and I saw this:

public class A {
    public A(SomeObject obj) {
      //Do something
    }
    //Some stuff
  public static class B {
    //Some other stuff
  }
}

I was wondering since even the inner class is public why have it as nested and not a separate class? Also, can I do this here: new A.B(SomeObject) ? I feel this defeats the purpose of a static class but I saw this implementation as well so wanted to know.

Spitler answered 22/8, 2012 at 16:57 Comment(0)
P
18

I was wondering since even the inner class is public why have it as nested and not a separate class?

That's really a matter to ask whoever wrote the class. It can allow the outer class to act as a "mini-namespace" though - if the nested class is only useful in the context of the outer class, it seems reasonable. It indicates deliberate tight coupling between the two classes. I most often see this in the context of the builder pattern:

Foo foo = new Foo.Builder().setBar(10).build();

Here it makes sense to me to have Foo.Builder nested within Foo rather than as a peer class which would presumably be called FooBuilder.

Note that it also gives some visibility differences compared with just unrelated classes.

Also, can I do this here: new A.B(SomeObject) ?

No, because B doesn't have a constructor with a SomeObject parameter - only A does (in the example you've given).

I feel this defeats the purpose of a static class

You should try to work out exactly what you deem the purpose of a static class to be, and in what way this defeats that purpose. Currently that's too vague a statement to be realistically discussed.

Profane answered 22/8, 2012 at 17:1 Comment(0)
K
5

You would have an inner class like this so

  • you can keep an class which only exists to support the outer class encapsulated.
  • you want to be able to access private members of the outer class or other nested classes.
  • you want a nested class with static fields (a weak reason I know ;)
  • you have a class with a very generic name like Lock or Sync which you wouldn't want to be mixed with other classes of the same name used by classes in the same package.

can I do this here: new A.B(SomeObject) ?

You can.

I feel this defeats the purpose of a static class

It takes getting used to but once you start you may have trouble not turning your entire program into one file.java ;)

Kist answered 22/8, 2012 at 17:1 Comment(0)
C
2

I was wondering since even the inner class is public why have it as nested and not a separate class?

Have a look at this thread: Why strange naming convention of "AlertDialog.Builder" instead of "AlertDialogBuilder" in Android

Also, can I do this here: new A.B(SomeObject) ?

(Update) No, you can't do this, since B doesn't have a constructor that asks for SomeObject.

I hope this helps.

Compensation answered 22/8, 2012 at 17:1 Comment(2)
Your link currently points to the last answer. I would suggest this link instead #11810845Doha
I think your answer is wrong for the new A.B(SomeObject) according to the other answers given below, which actually makes sense as there is no constructor for the static classSpitler
Z
2

1. A static inner class is known as Top-Level Class.

2. This static class has direct access to the Its Outer class Static method and variables.

3. You will need to initialize the static Inner class in this way from Outside...

    A a = new A();
    A.B b = new A.B();

4. new A.B(SomeObject) won't work... because you don't have a constructor with SomeObject as parameter...

5. But when the Inner class is Non-static, then it have implicit reference to the Outer class.

6. The outer and inner class can extends to different classes.

7. An interface's method can be implemented more than once in different or same ways, using Inner Class.

Zaccaria answered 22/8, 2012 at 17:14 Comment(0)
C
2

This pattern is used very often with the builder pattern. It not only makes clear the relation between a class and its builder, but also hides the ugly builder constructor/factory and makes builder more readable. For example in case you need your built object to have optional and not optional properties.

public class AnObject {
   public static class AnObjectBuilder {

       private AnObject anObject;

       private AnObjectBuilder() {
       }

       private void newAnObjectWithMandatory(String someMandatoryField, ...) {
           anObject = new AnObject(someMandatoryField,...)
       }

       public AnObjectBuilder withSomeOptionalField(String opt) {
           ...
       }
   }

   public static AnObjectBuilder fooObject() {
        return (new AnObjectBuilder()).newAnObjectWithMandatory("foo")
   }

   public static AnObjectBuilder barObject() {
        return (new AnObjectBuilder()).newAnObjectWithMandatory("bar")
   }
}

This way the client code have to call first the static method on the AnObjectBuilder class and then to use the optional builder methods:

AnObject.fooObject("foo").withSomeOptionalField("xxx").build(); without creating the builder object.

Pretty readable :)

Cementation answered 22/8, 2012 at 17:33 Comment(0)
L
0

I was wondering since even the inner class is public why have it as nested and not a separate class?

The simple reason it is allowed is packaging convenience.

Static nested class in Java, why?

http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html

yes, you can do new A.B(SomeObject). But you don't have to take my word for it, try it out.

Latter answered 22/8, 2012 at 17:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.