• 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

instance variable call vs method call

 
Ranch Hand
Posts: 658
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

output is:
Beta 44
4 44
Beta 44
44 44


output is correct but I am confuse in the way is it printed. At //line 1 b.h is called first so 4 should print firstly, but the method statement is printed firstly, why is it so ?
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's a great question. The reason is that Java evaluates the method parameters first before calling the method. Making the flow from Java' point of view:

  • What is b.h? 4
  • What is b.getH()? Well, when calling the method, print Beta 44. Then return 44.
  • Call println with both 4 and 44
  • Actually print both values in that order
  •  
    Enthuware Software Support
    Posts: 4810
    52
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Jeanne Boyarsky wrote:That's a great question.


    Thank you
     
    Marshal
    Posts: 8857
    637
    Mac OS X VI Editor BSD Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Puspender Tanwar wrote:output is correct but I am confuse in the way is it printed. At //line 1 b.h is called first so 4 should print firstly, but the method statement is printed firstly, why is it so ?

    Key point is, because method call "()" has higher precedence than member access "."

    1. It invokes method first - goes inside the method, executes print statement and (1)prints "Beta 44", returns value "44", and goes out of method.
    2. Executes print statement from left to right - (2)prints evaluated member variable value "4", and then (3)prints returned value from method "44".
     
    Puspender Tanwar
    Ranch Hand
    Posts: 658
    2
    Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Paul Anilprem wrote:Thank you


    hahaha ;)

    than you vilda & Jeanne Boyarsky
     
    Liutauras Vilda
    Marshal
    Posts: 8857
    637
    Mac OS X VI Editor BSD Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Puspender Tanwar wrote:

    Paul Anilprem wrote:Thank you


    hahaha ;)

    than you vilda & Jeanne Boyarsky

    Not sure what's so funny, but, have you understood what Jeanne told you and myself?
     
    Paul Anilprem
    Enthuware Software Support
    Posts: 4810
    52
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Puspender, if you post a question from a mock exam/book on Code Ranch, you need to quote the source from where you got it.

    Since I recognize that the question is from our material, I would like to suggest you to click on the "Discuss" button (if you are using our exam simulator) or click on the Question Id (if you are using Kindle eBook) to see a discussion on that particular question. In this case, it will take you to this page: http://enthuware.com/forum/viewtopic.php?f=2&t=2075
     
    Paul Anilprem
    Enthuware Software Support
    Posts: 4810
    52
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Liutauras Vilda wrote:

    Puspender Tanwar wrote:

    Paul Anilprem wrote:Thank you


    hahaha ;)

    than you vilda & Jeanne Boyarsky

    Not sure what's so funny, but, have you understood what Jeanne told you and myself?



    Sorry for the confusion, Liutauras. I wrote thank you with a wink because Jeanne said it was a great question and I recognized that it was from our material. So I took it as a compliment and hogged it
     
    Liutauras Vilda
    Marshal
    Posts: 8857
    637
    Mac OS X VI Editor BSD Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    All good Paul, I referred that not to you I just misunderstood whole laughing.

    And the question actually is very good and I'd say quite tricky if you're not sure about the precedences. Even if you do, quite easily can make mistake.
     
    Puspender Tanwar
    Ranch Hand
    Posts: 658
    2
    Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Paul Anilprem wrote:Puspender, if you post a question from a mock exam/book on Code Ranch, you need to quote the source from where you got it.


    yes, surely I will do.

    Liutauras Vilda wrote:Not sure what's so funny, but, have you understood what Jeanne told you and myself?


    I laughed because Paul caught me asking doubts from his material, and the whole scene was like, I was the thief and Paul the Bond007 caught me red handed

    anyways, Sorry Paul Anilprem
     
    Puspender Tanwar
    Ranch Hand
    Posts: 658
    2
    Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Paul Anilprem wrote: click on the Question Id (if you are using Kindle eBook) to see a discussion on that particular question. In this case, it will take you to this page: http://enthuware.com/forum/viewtopic.php?f=2&t=2075


    I was not aware of this.. it will be very helpful
    thank you
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    reply
      Bookmark Topic Watch Topic
    • New Topic