Results 1 to 4 of 4
  1. #1
    gib65 is offline Member
    Join Date
    Jun 2010
    Posts
    86
    Rep Power
    0

    Default 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.
    Attached Files Attached Files

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    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.

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,309
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by gib65 View Post
    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.
    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

  4. #4
    gib65 is offline Member
    Join Date
    Jun 2010
    Posts
    86
    Rep Power
    0

    Default

    Okay, now I understand a bit more about the Java run-time environment than I used to.

Similar Threads

  1. Cant figure out where went wrong.
    By leviathan in forum New To Java
    Replies: 15
    Last Post: 06-06-2010, 06:55 PM
  2. Need help - I can't figure it out.
    By Joshsmith in forum New To Java
    Replies: 2
    Last Post: 10-23-2009, 10:12 PM
  3. heap memory usage anomaly?
    By jon80 in forum Threads and Synchronization
    Replies: 1
    Last Post: 06-30-2009, 06:56 AM
  4. HashSet anomaly
    By jon80 in forum New To Java
    Replies: 1
    Last Post: 06-21-2009, 08:22 PM
  5. I can't figure this out
    By silvia in forum New To Java
    Replies: 3
    Last Post: 07-20-2007, 04:38 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •