static inner classes

T

Thomas Schodt

Tor said:
No, to _nested_ classes. Inner classes is a particular sort of nested
class that retains a non-null reference to an instance of its
enclosing class.

I'm gonna remember this terminology for a few weeks or possibly months.
Then I'll forget and go back to thinking of any class declared inside
another class as "inner", be it static or not.
I'll remember the difference the static keyword makes tho...
 
L

Larry Barowski

Andersen said:
Do I have to declare a (non-static) inner class inside the outer class
or can I do it in a separate file? The problem I have at the moment is
that I delcare the inner class in its own JAVA file, but then I cannot
reference the outer class member attributes. How can I solve this
problem? My files get too cluttered if I have all the inner classes
declared and defined inside the outer class, this class I am talking of
is already huge!

What do you mean by huge? Is it more than 15kloc?
I suspect you are worrying for no reason, but I could
be wrong.
 
J

John C. Bollinger

Dale said:
Unfortunately this terminology has changed over the years. The old
terminology used in the ammendments and clarifications to the original
JLS is confusing and contradictory. They have cleaned up the terminology
in the version 2 of the JLS. Here is the new terminology, which should
be used. Consider the old, confusing terminology of nested top-level
classes and static inner classes as deprecated.

- There are top-level classes and nested classes. There is no such thing
as a nested top-level class. A nested class is simply any class defined
within another class.

- Nested classes come in two flavors, static nested classes and inner
classes. Inner classes have an implicit link to an instance of the class
they are declared within. They cannot be instantiated apart from an
instance of their outer class. Static nested classes have no implicit
link to an instance of the outer class. The term static inner class is
now a contradiction in terms.

- Inner classes come in two varieties: named and anonymous.

Thanks, Dale, for the authoritative and full answer (exactly in line
with what I wrote). Thanks also for reminding us all of how some of the
confusion started in the first place.
 
D

Daniel Dyer

Thanks, Dale, for the authoritative and full answer (exactly in line
with what I wrote). Thanks also for reminding us all of how some of the
confusion started in the first place.

It's worth also pointing out that there is another, rarely used, type of
nested class - the local class. Josh Bloch calls the four types of nested
classes as static member classes, non-static member classes, anonymous
classes and local classes. Local classes are somewhere between non-static
member classes and anonymous classes.


public class MyOuterClass
{
public void myMethod()
{
// Class that can only be instantiated within this method.
class MyLocalClass
{
}

MyLocalClass myReference1 = new MyLocalClass();
MyLocalClass myReference2 = new MyLocalClass();
}
}


Dan.
 
C

Chris Uppal

Daniel said:
It's worth also pointing out that there is another, rarely used, type of
nested class - the local class.

Rarely used ? The anonymous class syntax is so revoltingly obfuscatory, and
the semantics are so limited, that I don't willingly use anything other than
"local classes" for inner classes.

-- chris
 
F

Furious George

Andersen said:
Do I have to declare a (non-static) inner class inside the outer class
or can I do it in a separate file? The problem I have at the moment is
that I delcare the inner class in its own JAVA file, but then I cannot
reference the outer class member attributes. How can I solve this
problem? My files get too cluttered if I have all the inner classes
declared and defined inside the outer class, this class I am talking of
is already huge!

I don't know if this would work or not. My java skills are merely
above average. Maybe some smarter people could say yea or nay.

Why not define your inner classes in separate interfaces? You can put
as many or as few inner classes in each interface. Then make the outer
class implement all the interfaces.

interface AInt { class AClass { } } ... interface ZInt { class ZClass {
} }
class Outer implements AInt ... ZInt { }
 
J

John McGrath

Inner classes have an implicit link to an instance of the class they are
declared within. They cannot be instantiated apart from an instance of
their outer class.

Actually, this is not quite right. Local classes and anonymous classes
that are declared in a static context are inner classes, but instances of
these classes do not have an enclosing class instance.
 
B

Bryan E. Boone

Tor said:
No, to _nested_ classes. Inner classes is a particular sort of nested
class that retains a non-null reference to an instance of its
encolsing class.
I've always hear that in:

public class A {
public static class B {
....
}
}

that B is referred to as a top-level nested class.
-Bryan
 
D

Dale King

It's in line with what you wrote, but what I wanted to point out was
that the people using the old terminology were not wrong, just out of
date. Originally they were called static inner classes and the oxymoron
of top-level nested classes.

Thankfully they cleaned up the terminology, but we have to understand
that people will still try to use the old terminology, because they
aren't aware of the change.
It's worth also pointing out that there is another, rarely used, type
of nested class - the local class. Josh Bloch calls the four types of
nested classes as static member classes, non-static member classes,
anonymous classes and local classes. Local classes are somewhere
between non-static member classes and anonymous classes.

I was lumping local classes in with nested classes, thinking that they
were really just nested classes with a smaller scope for the name, but
thinking about it some more and checking I think it could be considered
a special type as there are some slightly different rules (e.g. the
ability to access final local variables).

I'll include that, because what I posted is one of the canned response
for certain subjects that occur here frequently. Here is the added text:

- Named inner classes can be either class members (declared as a member
of a class) or local (declared within a method).
 
D

Dale King

Bryan said:
I've always hear that in:

public class A {
public static class B {
....
}
}

that B is referred to as a top-level nested class.

Originally that was what Sun called it, but that name is an oxymoron.
When they came out with the second edition of the JLS they cleaned up
the terminology and terms like static inner classes and top-level nested
classes are no longer used.

The new terminology is as follows:

- There are top-level classes and nested classes. There is no such thing
as a nested top-level class. A nested class is simply any class defined
within another class.

- Nested classes come in two flavors, static nested classes and inner
classes. Inner classes have an implicit link to an instance of the class
they are declared within. They cannot be instantiated apart from an
instance of their outer class. Static nested classes have no implicit
link to an instance of the outer class. The term static inner class is
now a contradiction in terms.

- Inner classes come in two varieties: named and anonymous.

- Named inner classes can be either class members (declared as a member
of a class) or local (declared within a method).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,778
Messages
2,569,605
Members
45,238
Latest member
Top CryptoPodcasts

Latest Threads

Top