Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > statics in inner classes

Reply
Thread Tools

statics in inner classes

 
 
Roedy Green
Guest
Posts: n/a
 
      07-01-2005
I think I almost have it understanding inner, nested, top-level,
public etc classes and the equivalent enums.

What I don't understand is the restriction on no static variables in
inner nested non-static instance classes. Why?

What puzzles me is it seems ok to derive inner classes from a class
that has some static variables. It only seems upset about new statics.

I finally figured out why you can only access final enclosing local
variables in your anonymous inner classes.

I am gradually putting it together and will post in the next day or
two at http://mindprod.com/jgloss/innerclasses.html

The other puzzle is why enums constructors cannot access static
fields.



--
Bush crime family lost/embezzled $3 trillion from Pentagon.
Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
http://www.infowars.com/articles/us/...s_rumsfeld.htm

Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes
 
Reply With Quote
 
 
 
 
Chris Smith
Guest
Posts: n/a
 
      07-01-2005
Roedy Green <(E-Mail Removed)> wrote:
> What I don't understand is the restriction on no static variables in
> inner nested non-static instance classes. Why?


It appears to be just an arbitrary rule. There is certainly an obvious
behavior that could have been assigned to it, but Sun decided against
it, for whatever reason. I wouldn't hunt for clues to anything
profound.

> What puzzles me is it seems ok to derive inner classes from a class
> that has some static variables. It only seems upset about new statics.


Since static members are not inherited, that's really not a problem.

> The other puzzle is why enums constructors cannot access static
> fields.


I'm unaware of such a restriction. At least, the following code
compiles with Eclipse 3.1RC3.

public enum Test
{
A(42), B(7)

;

static int a = 5;
public int aa;

Test(int val)
{
aa = val * a;
}
}

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

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
Reply With Quote
 
 
 
 
Lasse Reichstein Nielsen
Guest
Posts: n/a
 
      07-01-2005
Chris Smith <(E-Mail Removed)> writes:

> Roedy Green <(E-Mail Removed)> wrote:
>> The other puzzle is why enums constructors cannot access static
>> fields.

>
> I'm unaware of such a restriction. At least, the following code
> compiles with Eclipse 3.1RC3.


But did you test it? Both Test.A.aa and Test.B.aa has the value 0.

> public enum Test
> {
> A(42), B(7)


This corresponds to the declarations:

public static final A = new Test(42);
public static final B = new Test(42);

however ...

> static int a = 5;


the static variable "a" only gets its value later, so at the time of
creation of A and B, it has the default value.

To confuze it even more, had you made "a" final, it would have been a
constant, and would be inlined without a problem.


I'm not sure what Roedy Green's problem is, excatly. Enum constructors
can access static fields just fine, they just have to consider when
they are doing it ... which is before static initialiation reaches
the assignments to static fields.

/L
--
Lasse Reichstein Nielsen - http://www.velocityreviews.com/forums/(E-Mail Removed)
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
 
Reply With Quote
 
Chris Smith
Guest
Posts: n/a
 
      07-01-2005
Lasse Reichstein Nielsen <(E-Mail Removed)> wrote:
> > I'm unaware of such a restriction. At least, the following code
> > compiles with Eclipse 3.1RC3.

>
> But did you test it? Both Test.A.aa and Test.B.aa has the value 0.


Ah. I missed that; another ugly implementation artifact. Thanks for
pointing it out!

Unfortunately, these kinds of things are building up, making Java into a
language that has to be learned from version 1.0 forward, instead of
making sense statically in its current form.

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

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      07-02-2005
On Fri, 1 Jul 2005 09:34:38 -0600, Chris Smith <(E-Mail Removed)> wrote
or quoted :

>It appears to be just an arbitrary rule. There is certainly an obvious
>behavior that could have been assigned to it, but Sun decided against
>it, for whatever reason. I wouldn't hunt for clues to anything
>profound.


I've been chewing on it. One thing that hit me is it may speed garbage
collection. Classes I think are harder to scavenge that object. You
don't want millions of little throwaway class objects littering the
pool. By refusing static members, you can dispense with a separate
class object for each anonymous class.

--
Bush crime family lost/embezzled $3 trillion from Pentagon.
Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
http://www.infowars.com/articles/us/...s_rumsfeld.htm

Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes
 
Reply With Quote
 
Chris Smith
Guest
Posts: n/a
 
      07-02-2005
Roedy Green <(E-Mail Removed)> wrote:
> I've been chewing on it. One thing that hit me is it may speed garbage
> collection. Classes I think are harder to scavenge that object. You
> don't want millions of little throwaway class objects littering the
> pool. By refusing static members, you can dispense with a separate
> class object for each anonymous class.


No, you definitely need a separate Class object (and all the other
underlying class stuff) for each anonymous inner class. Anonymous
classes are treated in exactly the same way as any other class, and the
JVM generally doesn't even pay attention to the difference.

I don't think it would matter anyway. Class GC is done per class
loader, so unless you added additional class loaders, there isn't any
additional work to be done in garbage collection. An anonymous class is
ALWAYS loaded by the same classloader as its containing class (or
conceivably by its ancestor, but that requires strange contortions).

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

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
Reply With Quote
 
Raymond DeCampo
Guest
Posts: n/a
 
      07-02-2005
Chris Smith wrote:
> Roedy Green <(E-Mail Removed)> wrote:
>
>>What I don't understand is the restriction on no static variables in
>>inner nested non-static instance classes. Why?

>
>
> It appears to be just an arbitrary rule. There is certainly an obvious
> behavior that could have been assigned to it, but Sun decided against
> it, for whatever reason. I wouldn't hunt for clues to anything
> profound.
>


According to the specification, inner classes may have static fields
that are compile time constants. So there's one of those clues you
suggested we not hunt for.

Ray

--
XML is the programmer's duct tape.
 
Reply With Quote
 
Thomas G. Marshall
Guest
Posts: n/a
 
      07-09-2005

"Chris Smith" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed).. .
> Roedy Green <(E-Mail Removed)> wrote:
>> What I don't understand is the restriction on no static variables in
>> inner nested non-static instance classes. Why?

>
> It appears to be just an arbitrary rule. There is certainly an obvious
> behavior that could have been assigned to it, but Sun decided against
> it, for whatever reason. I wouldn't hunt for clues to anything
> profound.


Me neither, but I would offer a possibility. Well actually, more
accurately, they are stray thoughts forming in me head.

Throughout the documentation, sun seems to regard inner classes as members
of enclosing instances. Basically, even though it looks like a simple class
namespace issue, it actually regards the inner class as a member, with rules
similar to other members. I of course am very likely to have this wrong,
but it seems this way, perhaps I'll go dig up what I'm referring to today.

Take for example, one of the goofiest things in java:

X.Y xy = new X().new Y();

For when Y is a non-static class it requires the context of the X /object/
to instantiate it. Where as when Y is a static inner:

X.Y xy = new X.Y();

Works just fine. So going back to:

new X().new Y();

The inner class Y is (perhaps?) not able to have a static member because
there is no static class for it to belong to? That is, X, the outer class
is (perhaps?) by definnition an implied static. The inner however, is not,
and therefore there is no class per se to actually contain the statics.

This is far from a connecting of the dots, and is likely wrong, but it's the
best I can do right now.







>
>> What puzzles me is it seems ok to derive inner classes from a class
>> that has some static variables. It only seems upset about new statics.

>
> Since static members are not inherited, that's really not a problem.
>
>> The other puzzle is why enums constructors cannot access static
>> fields.

>
> I'm unaware of such a restriction. At least, the following code
> compiles with Eclipse 3.1RC3.
>
> public enum Test
> {
> A(42), B(7)
>
> ;
>
> static int a = 5;
> public int aa;
>
> Test(int val)
> {
> aa = val * a;
> }
> }
>
> --
> www.designacourse.com
> The Easiest Way To Train Anyone... Anywhere.
>
> Chris Smith - Lead Software Developer/Technical Trainer
> MindIQ Corporation



 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      07-10-2005
On Sat, 09 Jul 2005 16:02:48 GMT, "Thomas G. Marshall"
<(E-Mail Removed). com> wrote or quoted
:

> The inner however, is not,
>and therefore there is no class per se to actually contain the statics.


If there were statics for inner classes what are they -- one class per
set of objects tied to mother? or to me more logical one class per set
of such objects ever created anywhere.


What happens when your inner class extends a class that already has
some statics? Does it stop you?

--
Bush crime family lost/embezzled $3 trillion from Pentagon.
Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
http://www.infowars.com/articles/us/...s_rumsfeld.htm

Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes
 
Reply With Quote
 
Lasse Reichstein Nielsen
Guest
Posts: n/a
 
      07-10-2005
Roedy Green <(E-Mail Removed)> writes:

> If there were statics for inner classes what are they -- one class per
> set of objects tied to mother?


That would be consistent. A member class belongs to the enclosing
instance, so there really is one class per enclosing instance. It's
just not implemented like that under the hood, but that is the idea.

> What happens when your inner class extends a class that already has
> some statics? Does it stop you?


Extending a class that contains static members will allow you to
access the members through the extending type as well. But that's just
being nice. They are still resolved, at compile time, to the static
members of the class declaring them, and the compiled bytecode need
not refer to the extending class at all.

/L
--
Lasse Reichstein Nielsen - (E-Mail Removed)
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
failing to instantiate an inner class because of order of inner classes Pyenos Python 2 12-27-2006 11:19 PM
lifetime of global statics vs. statics in functions Stuart MacMartin C++ 5 07-27-2005 04:19 PM
What is the difference between nested classes and inner classes ? Razvan Java 5 07-27-2004 07:59 PM
How to access inner classes variables & methods from outer classes lonelyplanet999 Java 1 11-13-2003 01:54 PM
inner classes in python as inner classes in Java Carlo v. Dango Python 14 10-19-2003 08:49 AM



Advertisments