This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Svelte and Sapper in Action and have Mark Volkmann on-line!
See this thread for details.
Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Method calls as parameters

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am taking a practice test for Java certification and got this question:

What will the following code print?



This is the answer:
Beta 44
4 44
Beta 44
44 44


I understand the part about overriding and h being shadowed or not. What I'm confused about is what exactly happens when getH() is passed as a parameter. When it is passed as a parameter, does it get to output its println()? Apparently it does. So then why is "Beta 44" first? Why does getH() get executed before b.h is printed?

Thanks for any help.
 
author & internet detective
Posts: 40200
816
Eclipse IDE VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Patrick Kasarski wrote: So then why is "Beta 44" first? Why does getH() get executed before b.h is printed?


Because Java needs to know the values to pass to System.out.println(). The value of b.h is already known since it is just a variable. Since getH() is a method, it needs to be executed. Then both (b.h and the result of getH()) get passed to println at the same time. It isn't until that point that it executes.
 
Patrick Kasarski
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jeanne Boyarsky wrote:It isn't until that point that it executes.



Thank you. So just to clarify, if you call a method as a parameter, the parameter method will execute first, before the method call of which it is a parameter?
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Patrick Kasarski :
if you call a method as a parameter, the parameter method will execute first, before the method call of which it is a parameter?




Thats absolutely true . In your example also , the same thing happens .
 
Did you just should on me? You should read this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic