Reputation: 7844
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.
Upvotes: 16
Views: 14168
Reputation: 9599
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.
Upvotes: 2
Reputation: 15230
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 :)
Upvotes: 2
Reputation: 33534
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.
Upvotes: 2
Reputation: 5661
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.
Upvotes: 0
Reputation: 1500055
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.
Upvotes: 18
Reputation: 533492
You would have an inner class like this so
private
members of the outer class or other nested classes.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 ;)
Upvotes: 5