• Post Reply Bookmark Topic Watch Topic
  • New Topic

instance variable call vs method call  RSS feed

 
Ranch Hand
Posts: 525
2
Java
  • Mark post as helpful
  • send pies
  • 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
Marshal
Posts: 37698
575
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • 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
    Ranch Foreman
    Posts: 4132
    34
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Jeanne Boyarsky wrote:That's a great question.


    Thank you
     
    Sheriff
    Posts: 5066
    357
    BSD
    • Mark post as helpful
    • send pies
    • 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: 525
    2
    Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Paul Anilprem wrote:Thank you


    hahaha ;)

    than you vilda & Jeanne Boyarsky
     
    Liutauras Vilda
    Sheriff
    Posts: 5066
    357
    BSD
    • Mark post as helpful
    • send pies
    • 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
    Ranch Foreman
    Posts: 4132
    34
    • Mark post as helpful
    • send pies
    • 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
    Ranch Foreman
    Posts: 4132
    34
    • Mark post as helpful
    • send pies
    • 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
    Sheriff
    Posts: 5066
    357
    BSD
    • Mark post as helpful
    • send pies
    • 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: 525
    2
    Java
    • Mark post as helpful
    • send pies
    • 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: 525
    2
    Java
    • Mark post as helpful
    • send pies
    • 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
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!