Re: why do we need "static" inner classes??
- From: neuneudr@xxxxxxxx
- Date: 11 Oct 2006 12:38:28 -0700
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
.
- Follow-Ups:
- Re: why do we need "static" inner classes??
- From: snehaltiwari
- Re: why do we need "static" inner classes??
- References:
- why do we need "static" inner classes??
- From: snehaltiwari
- why do we need "static" inner classes??
- Prev by Date: Re: Initialize array without knowing it size?
- Next by Date: Re: Error when creating BufferedImage with IndexColorModel from a byte array.
- Previous by thread: why do we need "static" inner classes??
- Next by thread: Re: why do we need "static" inner classes??
- Index(es):
Relevant Pages
|