Hi all,
I am a newbie in java and have prior OO experience in PB and .Net.
The term "static" means that a class member belongs to the definiton
and not the instance. i.e. it is shared across all instances.
Then, why does the language allow for "static" nested classes which
need to be instantiated to be used??
As far as I understand, the resoning behiend nested classes is to group
functionality into related classes (reducing no of external classes),
increase encapsulation and having more readable code.
As we design our applications, why would we need a nested class (i.e.
static) that is not tied to its parent instantance?
It seems, having a static nested class is equivalent to having a top
level class without any specific language/programming features.
It feel it would only increases complexity in understanding and
implementing them.
Please correct me if I am wrong in my underrstanding.
Would be great to have any comments from ppl having used/implemented
nested classes.
Regards
opalpa opalpa@gmail.com http://opalpa.info - 11 Oct 2006 14:40 GMT
> Hi all,
>
[quoted text clipped - 15 lines]
> It seems, having a static nested class is equivalent to having a top
> level class without any specific language/programming features.
One difference is that static inner class' access can be limited more.
So if you want a class that is A) only available for use by a certain
top level class and B) does not have a reference to the top level
class, then you use a static inner class.
Seperatly, sometimes a class only makes sense in context of another
class but does not need to have a reference to top level class. You
could make two top level classes TopLevelClass and
TopLevelClassSymbiont or you could make TopLevelClass and an inner
class which could be refered to as TopLevelClass.Symbiont. Putting the
inner class into the top level class marks the context relationship
mentioned in the first sentance of this paragraph.
Seperatly, occasionally, with Java 5.0 import static, I use a top level
classes to group public classes.
> It feel it would only increases complexity in understanding
A new concept indeed needs be understood. The compensating factor is
that it can reduce complexity of code.
> and implementing them.
Features take effort.
opalpa
opalpa@gmail.com
http://opalpa.info/
neuneudr@yahoo.fr - 11 Oct 2006 20:38 GMT
> Hi all,
>
[quoted text clipped - 5 lines]
> Then, why does the language allow for "static" nested classes which
> need to be instantiated to be used??
Why do some OO languages only support implementation inheritance?
Why do some other OO languages prevent implementation inheritance?
Why do some OO languages allow other types of polymorphism than
subtype polymorphism?
etc.
Note that anyway what you say isn't necessarly true: if your
static nested class contains, for example, a static field
(JLS 8.3.1.1: "static fields") you do not necessarly need to
have an instance of the nested class to access that field.
> As far as I understand, the resoning behiend nested classes is to group
> functionality into related classes (reducing no of external classes),
> increase encapsulation and having more readable code.
>
> As we design our applications, why would we need a nested class (i.e.
> static) that is not tied to its parent instantance?
You're confused on the terminology...
You ask "why do we need 'static' inner classes??" in the
subject then "why would we need a nested class (i.e. static)...".
It seems like you think that nested imply static.
You've got it backward : "inner" means "not static".
There's no such thing as a "static inner" class in Java.
Inner classes are nested classes.
A static nested class is NOT an inner class.
An inner class can NEVER be static.
A nested class is either static or inner.
The *very first time* the term "inner class" appears in the JLS it
is defined like this:
"An inner class is a nested class that is not explicitly
or implicitly declared static"
http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.1.3
And "inner class" implies that there's an enclosing instance.
Not surprisingly, that section of the JLS is called :
"8.1.3 Inner Classes and Enclosing Instances"
Do not pay attention to supposedly common usage. There may be
many people mixing up nested/inner... But there are also many
experienced people who do not mix these terms.
The Java Language Specification is authoritative on that subject.
It helps communication to use the correct terms.
See you later,
Driss
snehaltiwari@gmail.com - 12 Oct 2006 08:57 GMT
Thanks both of you for your comments and clarifying the terminology and
usage.
Driss,
> As we design our applications, why would we need a nested class (i.e.
> static) that is not tied to its parent instantance?
I should have used alternative means to express what I meant here...
When i mentioned 'nested class' above I meant "a class that is enclosed
within another class" and instead of "(i.e. static), I should have said
"(which is static)".
Being a newbie, I have not gone through / referred to JLS yet.
For my understanding, I read the article in the following link
http://java.sun.com/docs/books/tutorial/java/javaOO/nested.html
Thanks for laying out the bare bones of nested/inner classes in simple
terms.
-Snehal