aspose file tools*
The moose likes Java in General and the fly likes inner anonymous class fields initialization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "inner anonymous class fields initialization" Watch "inner anonymous class fields initialization" New topic
Author

inner anonymous class fields initialization

Gasan Guseynov
Ranch Hand

Joined: Jan 03, 2006
Posts: 67
Hi, folks. I have 2 inner anonymous classes (Class1, Class2):



java 1.4

What's the point?!


And another question.
why I can have static fields of primitive type or string type in inner anonymous class, but I couldn't have static fields of any other type?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

There's a rather lot missing from your example to make it runnable; here's my attempt at completing it:


When I run this, I get



So: it works fine for me.

As to your second question: anonymous inner classes can't have static fields of any type. You'd have to show us a working example to illustrate otherwise.


[Jess in Action][AskingGoodQuestions]
Gasan Guseynov
Ranch Hand

Joined: Jan 03, 2006
Posts: 67
about first: strange, but I'll try to make the code more similar to what I have and what really doesn't work.
about second: consider adding fields:

to Class2 anonymous descendant. It works.

Btw, thanks for adding main and classes declaration.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

So, for example:



Compiling this, I get

Gasan Guseynov
Ranch Hand

Joined: Jan 03, 2006
Posts: 67
I'm not cheating you.


works perfectly

intellij idea 7.05, jdk 1.4


Output is:
A
11
C
java.lang.Object@74c3aa
java.lang.Object@1d9fd51
Gasan Guseynov
Ranch Hand

Joined: Jan 03, 2006
Posts: 67
It also works when compiling from command line under jdk 1.6 with the same result.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9319
    
  17

Inner classes can have static final members...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Gasan Guseynov
Ranch Hand

Joined: Jan 03, 2006
Posts: 67
what I'm trying to say is that is legal to have static member in inner anonymous class only of primitive type or String type, but only of them. Why?!
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9319
    
  17

Gasan you can have static members in inner classes only if they are final no matter what the datatype is. This is only not applicable to static inner classes...
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Ah, OK. Inner classes are allowed to have static "members" that are compile-time constants, because the values can just be substituted directly into the code. I'm not sure that storage is even allocated for these members; they're treated like macros. The Java Language Spec allows compile-time constants of any primitive type plus Strings.
Gasan Guseynov
Ranch Hand

Joined: Jan 03, 2006
Posts: 67
look:




C:\>javac Class0.java
Class0.java:18: inner classes cannot have static declarations
private static final Object aa;
^
1 error


jdk 1.6
Gasan Guseynov
Ranch Hand

Joined: Jan 03, 2006
Posts: 67
Ernest, I don't clearly understand what you mean by saying that compile time static members constants treated as a macros. But, you're right, in other words, only string or number literals can be static member constants for anonymous inner class (probably for all inner classes, but I don't know).
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9319
    
  17

Compile time constants are final members which are assigned a value at the time of declaration. Eg

final int i;

is not a compile time constant, but the following is

final int i = 10;
Gasan Guseynov
Ranch Hand

Joined: Jan 03, 2006
Posts: 67
Ankit, I know what the compile time constant is, ok?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40034
    
  28
Gasan Gouseinov wrote:Ankit, I know what the compile time constant is, ok?
Now, now, that is hardly a "nice" way to talk. Please look at this.

And I hardly think inner classes like this are a beginner's problem. Moving.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Gasan Gouseinov wrote:Ernest, I don't clearly understand what you mean by saying that compile time static members constants treated as a macros. But, you're right, in other words, only string or number literals can be static member constants for anonymous inner class (probably for all inner classes, but I don't know).


The compiler can substitute the value right into the code. So instead of there being the bytecode to fetch the value of the member, there will just be the literal value (or for a String, the code to fetch the literal value directly from the class's constant pool).
Paul Yule
Ranch Hand

Joined: May 12, 2008
Posts: 229
Gasan Gouseinov wrote:what I'm trying to say is that is legal to have static member in inner anonymous class only of primitive type or String type, but only of them. Why?!


I'm speculating:

I believe you would have problems with String were it not a String "literal".

it seems as though static members of a non-static inner class doesn't make sense. Since each instance of the inner class is tied to the instance of its outer class how would you access this static member (I'm actually asking)? Thus this is not permitted at all. However, since compile time constants-- primitives that are given a value when they are declared and then dubbed final--cannot change the compiler can remove ocurrences of the variable and replace it with the value. Static or otherwise seems to make no functional difference in scope as far as I see.


edit: thought too long about my response and Earnest beat me.
Gasan Guseynov
Ranch Hand

Joined: Jan 03, 2006
Posts: 67
Wow, wow. Stop guys, if I insulted someone, I'm really sorry.
Ritchie, about problem complexity, I didn't know, how to determine to which java thread to post, so posted to beginner. (Bcs problem isn't about neuronal networks or nanopipes (sic)).
Ernest, at least, I managed to instantiate some code which is resembling what I have and what produced the problems. And it is turned out that the problem isn't very complex and is obvious in some way. Here is the code which generate the same exceptions, which I had.


so the problem is because of parent constructor wasn't completed, therefore there's no way to run child inner class constructor which will initialize instance fields. I didn't found the best solution for this. Probably, it's to add methods for that fields, which will initialize them if they wasn't. I'll find the solution. The most important thing is that I found the reason of this behavior. Many thanks, guys.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Gasan Gouseinov wrote:I didn't found the best solution for this. Probably, it's to add methods for that fields, which will initialize them if they wasn't.


I don't think there really is a rule or solution that always prevents this kind of problem; you really just have to be aware of the possibility, and avoid it. Many people recommend never calling a non-final method of a class during a constructor, to avoid some similar problems (because if you call a method overridden in the child, the child's members won't have been initialized yet.)

In this case, if you want the child to initialize members of the parent, it'd be best to provide an abstract method named "init()" or something like that, then deliberately call it from the parent constructor before accessing those members; i.e.,



But I run into this kind of problem now and then. The classic version is a JDialog subclass that calls setVisible() in its constructor, so that it automatically shows itself. Great idea, right? Now someone writes a subclass of the subclass -- and the dialog becomes visible before the child class constructor has fully run. This can cause some interesting race conditions!
Gasan Guseynov
Ranch Hand

Joined: Jan 03, 2006
Posts: 67
Many thanks, Ernest.
Gasan Guseynov
Ranch Hand

Joined: Jan 03, 2006
Posts: 67
It seems that you're right, Paul. Because I don't know any possibility to access static fields of anonymous class and I don't know the way to access non-static fields of anonymous class either. So, from utilization point of view, it seems that as you said, static and non-static field of anonymous class are identical. But the byte code generated for this 2 different situations not identical: statical identifier presence or absence is preserved.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: inner anonymous class fields initialization