static inner classes

A

Andersen

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!

regards,
Andersen
 
A

Andersen

Sorry about the Subject line, it is misleading, I am talking about
non-static inner classes, not static ones (I do want to refer to the
outer class member attributes).
 
B

Boudewijn Dijkstra

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!

The Java compiler normally copies a reference of the outer class object, to
the inner class, via the constructor. This behaviour can easily be duplicated
in separate inner and outer source files. If you want more details, you can
disassemble a simple inner class.
 
A

Andersen

Boudewijn said:
"Andersen" <[email protected]> schreef in bericht

The Java compiler normally copies a reference of the outer class object, to
the inner class, via the constructor. This behaviour can easily be duplicated
in separate inner and outer source files. If you want more details, you can
disassemble a simple inner class.

Does this relly work? What if the member attributes are of primitive
type? Then you are making a deep/defensive copy to the inner class, and
a change in any of the instances will not affect the other!
 
W

Wibble

I think the point is, inner classes generate separate
byte code files, so you can write your own compiler or
byte code editor.

Of course, its alot simpler to define the 'inner' class
as a regular class thats just references the 'outer'
instance. You can't do the same visibility hacks as
with inner classes, but they're a little smelly anyway.

Inner classes are really just syntactic sugar.
 
B

Boudewijn Dijkstra

Andersen said:
Does this relly work? What if the member attributes are of primitive type?
Then you are making a deep/defensive copy to the inner class, and a change
in any of the instances will not affect the other!

Just a reference to the outer class object is copied, not the members!
 
T

Thomas Schodt

John said:
That's the only kind. Inner classes cannot be static.

The static keyword can be applied to inner classes.
[Whatever that means.]


To OP.

<file: Inner.java>
class Inner {
...
}
</file>

<file: Outer.java>
class Outer {
Inner getInstance() {
return new Inner() {
{ // instance initializer block [anonymous class <init>()]
// super(); - this would be implied? [try it]
}
void fubar() {
}
};
}
}
</file>

This should work.

Any methods that access Outer fields
have to have their body in the anonymous inner class.

Remaining methods can be coded in Inner.java

It may be simpler to just add getter/setter methods?
 
J

John McGrath

No, they cannot. See JLS §8.1.2:

An inner class is a nested class that is not explicitly
or implicitly declared static.

The correct term for a class declared within another class declaration is
a *nested* class, not an *inner* class. An inner class is one that has an
enclosing class instance.
 
C

Chris Smith

I didn't quote the article I was reffering to at first. I was just
saying that yes, inner classes can be static.

This is simply not the case.

A nested class that is declared static is *not* an inner class. By
definition, an inner class implicitly exists within the context of an
instance of its outer class. By adding the 'static' keyword to
something that would otherwise be an inner class, you have made it no
longer an inner class; rather, it's only a nested class.

Nested classes are any class that's not top-level. Inner classes are a
*subset* of nested classes, which aren't static. So it makes sense to
talk about a static nested class, but not a static inner class. There
was some confusion back in the 1.1 release cycle about this terminology,
but Sun clarified it about six or seven years ago, and it's been the
same since.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
A

Andersen

Boudewijn said:
Just a reference to the outer class object is copied, not the members!

How can it be a syntactic sugar, if the sugar translates to having
references to the outer class member attributes, what happens to the
outer class primitive attributes. They can be accessed, modified (given
they're not final) by the inner class, but if you are just making a
class that copies the references you cannot achieve this. I suspect
something else is going on here.

regards,
Andersen
 
J

John C. Bollinger

I didn't quote the article I was reffering to at first. I was just
saying that yes, inner classes can be static.

No, they cannot, but it's a matter of terminology. Inner classes are
"nested classes" that are /not/ static. The other kind of nested
classes are simply "static nested classes".
 
T

Tor Iver Wilhelmsen

Thomas Schodt said:
The static keyword can be applied to inner classes.

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.
 
J

John C. Bollinger

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!

At the source code level, you can create a class with many of the
semantics of an inner class without lexically nesting it inside its
outer class, but as a matter of terminology, it is not then an "inner
class". The Java compiler does a fair amount of magic in some cases to
make bytecode for nested classes that behaves according to the specs,
and you may have to implement some of that directly in code if you want
a class that has behavior similar to an inner class but whose source is
not lexically nested in its "outer class"'s source.

You may, however, want to consider whether you could convert some of
your inner classes into standalone classes, which then would not need to
nest. In considering this refactoring, you might want to examine
whether some of the top-level class' attributes might more appropriately
belong to one or another of the currently nested classes -- if so (and I
would expect it) then you may be able to resolve the attribute access
issues by assigning the attributes to the appropriate classes.
 
B

Boudewijn Dijkstra

Andersen said:
How can it be a syntactic sugar, if the sugar translates to having
references to the outer class member attributes, what happens to the outer
class primitive attributes. They can be accessed, modified (given they're
not final) by the inner class, but if you are just making a class that
copies the references you cannot achieve this. I suspect something else is
going on here.

Please read what I wrote. The outer class members (reference or primitive)
are not copied, just a reference to the object holding these members (a.k.a.
the outer class object).

For access to private members of the outer class, accessor methods are usually
written by the compiler.
 
J

John McGrath

How can it be a syntactic sugar, if the sugar translates to having
references to the outer class member attributes, what happens to the
outer class primitive attributes. They can be accessed, modified (given
they're not final) by the inner class, but if you are just making a
class that copies the references you cannot achieve this. I suspect
something else is going on here.

Assume we have a top-level class named Top that contains an inner class
Top.Inner. You can transform it to a non-inner class as follows:

1) Move the declaration of class Inner from inside of Top to outside.
2) Add a private field (say "outer") of type Top to class Inner.
3) Add an argument of type Top to each constructor in class Inner.
4) In each constructor (that does not call another Inner constructor), set
the value of the field "outer" from the argument value.
5) Change all references to Top members in Inner to use the "outer" field

This is essentially what the compiler does. There were almost no changes
in the JVM specification required to implement nested (including inner)
classes.
 
P

Patricia Shanahan

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!

You have a class that is already huge, and still growing. It
is so huge that its size is getting in the way of the
natural implementation of the new code. To me, your question
yells "REFACTOR!".

Make sure you are not just assuming that the right division
is between the code you already have and the code you are
adding. Maybe there is a better split that would group the
new code with some, but not all, of the existing code.

If the split between the new and the old code really is the
best possible, presumably the new code is less tightly
coupled to the rest of the code than any other piece in the
existing class. Consider making it a separate class, with
restricted interfaces to the existing class, rather than
emulating an inner class. A large volume of code with common
access to the same pool of attributes can be a maintainance
nightmare.

Patricia
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top