Re: why do we need "static" inner classes??



snehaltiwari@xxxxxxxxx wrote:
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??

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

.



Relevant Pages

  • Re: a sort of multiple inheritance
    ... But I can't find a way to limit instanciation of the nested class to ... What that code is doing is creating a delegate representing a factory method that can instantiate the class with the private constructor, and storing a reference to that in the outer class where the Journal class can get at it, but no other code can. ... In particular, the static initializer shown isn't going to execute until the inner type is actually accessed somehow. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: static inner classes
    ... Inner classes cannot be static. ... A nested class that is declared static is *not* an inner class. ...
    (comp.lang.java.programmer)
  • Re: Nested classes
    ... The inner class is a whole own thing. ... my inner class is declared in the private section of Outer of course. ... The nested class is not even visible to the public, ... What's am I gaining with declaring ...
    (comp.lang.cpp)
  • Re: Access outer class members from a nested class
    ... the JAVA syntax of Outer.this.m_OuterValue does not exist apparently. ... The type of nested class you appear to be thinking of is an "inner class". ... An inner class has an implicit reference to an instance of the outer class, determined at the time of instantiation, based on the instance of the outer class that created the inner class. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Access outer class members from a nested class
    ... outer class says you can't access non-static members. ... less explicit than the Java approach. ... light of the nested class behaving as a static member, ...
    (microsoft.public.dotnet.languages.csharp)