Results 1 to 4 of 4
- 10-01-2010, 05:28 PM #1
Member
- Join Date
- Jun 2010
- Posts
- 86
- Rep Power
- 0
see if you can figure out this 'anomaly'
Please download, compile, and run the two attachments. Tell me if you can explain this 'anomaly'.
I post this more as a curiocity than a need to solve a problem (the second attachment, judging by the title, would suggest I've found a solution). It makes me wonder what the java run-time environment (or maybe the compiler) is doing in this case. Regardless of the fact that there's a simple way to remedy it, it seems to me (in my limited state of Java expertise) that there really shouldn't be an error to begin with.
- 10-01-2010, 07:08 PM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Because super is run first, then all variables are defined as per the class definition, then the rest of the constructor runs. Since you do not give the instance variable a value in the "no-error" you do not overwrite the value given in the "super" call. Since you do give the instance variable a value in the "error" version you overwrite the value defined in the super call. This is the main reason why all methods called from a constructor should be private and/or final.
If you walk through the stuff in a debugger you would see this.
- 10-02-2010, 11:56 AM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
There is no anomaly, it is just your lack of understanding how the jvm initializes objects: just before the body of a constructor is executed a superclass constructor is executed (this applies recursively), next all explicit member variables are initialized and only then the body of the ctor is executed. All the while the 'virtual function table' (a C++ ism) is already set up so a superclass ctor can call a method implemented in a subclass (a very bad thing, as already mentioned).
kind regards,
Jos
- 10-02-2010, 04:49 PM #4
Member
- Join Date
- Jun 2010
- Posts
- 86
- Rep Power
- 0
Similar Threads
-
Cant figure out where went wrong.
By leviathan in forum New To JavaReplies: 15Last Post: 06-06-2010, 06:55 PM -
Need help - I can't figure it out.
By Joshsmith in forum New To JavaReplies: 2Last Post: 10-23-2009, 10:12 PM -
heap memory usage anomaly?
By jon80 in forum Threads and SynchronizationReplies: 1Last Post: 06-30-2009, 06:56 AM -
HashSet anomaly
By jon80 in forum New To JavaReplies: 1Last Post: 06-21-2009, 08:22 PM -
I can't figure this out
By silvia in forum New To JavaReplies: 3Last Post: 07-20-2007, 04:38 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks