Re: final field init
Rémi Forax <[email protected]>
| Newsgroups | gmane.comp.java.objectweb.asm |
|---|---|
| Message-ID | <[email protected]> |
Le 28/09/2010 09:14, dlf a écrit : > Sorry for the newbie question. I haven't been able to track this issue down on > the internet, though I feel like I've read about it before. > > I'm generating a class with two public final member fields and an empty > constructor that does nothing but call java.lang.Object.<init>. > > When I load the class and print out the fields directly, they're fine. But > when I use reflection to access the fields, I get the default values for the > type, not the constant values I thought I'd assigned. > > When I view the newly-created instance (either method) using the eclipse > debugger, it thinks the fields have the default values. This despite the fact > that the next line that accesses the fields directly prints out the right > value. > > I wrote java source for a similar class with the final constant fields and an > empty constructor, and the (eclipse) compiler generated code to initialize > these fields inside the constructor (as reported using ASM TraceClassVisitor). > So I'm wondering if the behavior I'm seeing has to do with the difference > between these two ways of initializing the constant fields. The constant value of a field is intended to be used only for static field. If fields are final but not static, you have to insert initialization in all constructors. Rémi
message-footer.txt
(text/plain, 238 B)
-- You receive this message as a subscriber of the [email protected] mailing list. To unsubscribe: mailto:[email protected] For general help: mailto:[email protected]?subject=help OW2 mailing lists service home page: http://www.ow2.org/wws