• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

inner anonymous class fields initialization

 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Gasan Guseynov
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, for example:



Compiling this, I get

 
Gasan Guseynov
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It also works when compiling from command line under jdk 1.6 with the same result.
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Inner classes can have static final members...
 
Gasan Guseynov
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ankit, I know what the compile time constant is, ok?
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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).
 
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Many thanks, Ernest.
 
Gasan Guseynov
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic