• 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
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

Anonymous class Dan's Mock exam

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Referring to the following code below, the out put is 5. I am showing the flow here with the numbers as I understood. When I debug this code using IntelliJ, I could not follow the flow. Can any body clarify me if am wrong of if I am missing any thing.

Thankx in advance,
 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Seems like you negotiatied the tricky part perfectly, then slipped on the arithmetic? By your numbers:
  • 4: y = 2
  • 5: x = 2
  • 7: y++
  • 9: x + y
  •  
    Greenhorn
    Posts: 20
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I have another question,
    new A(2) {
    {incY();}
    //... code skipped
    };
    here how could the statements {incY();} compiled(though it does)? I understand only static method could be used this way, but it not declared as static, please enlighten me, thanks!
     
    Steve Lovelace
    Ranch Hand
    Posts: 125
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I assume you mean you know about static initializers. Instance initializers are there too, and the above is an example (does make you look twice though, doesn't it?).
     
    Dep Joy
    Greenhorn
    Posts: 20
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thank Steve, is this instance initializer on the SCJP objective list? As far as I can remember, it is not mentioned in both K&B and RHE which two books I have.
    [ September 19, 2003: Message edited by: Dep Joy ]
     
    Steve Lovelace
    Ranch Hand
    Posts: 125
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Right, I don't remember it mentioned in K&B, which is what I'm relying on, nor do I recall it popping up anywhere else but in this question. Might be a safe bet to ignore, but on the other hand, there's nothing to it. When a new instance is being initialized, after all its superclass initializations have completed: 1) its instance vars are initialized 2) any instance initializers are run 3) the constructor is run.
     
    Ranch Hand
    Posts: 787
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    posted by Steve:


    Right, I don't remember it mentioned in K&B, which is what I'm relying on, nor do I recall it popping up anywhere else but in this question. Might be a safe bet to ignore, but on the other hand, there's nothing to it. When a new instance is being initialized, after all its superclass initializations have completed: 1) its instance vars are initialized 2) any instance initializers are run 3) the constructor is run.


    Hi Steve:
    I thought it was done in textual order. Following will give forward reference error:
     
    Steve Lovelace
    Ranch Hand
    Posts: 125
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Didn't know that! Thanks!
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    reply
      Bookmark Topic Watch Topic
    • New Topic