Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Objects Creating the MVP Storing Guesses

Razvan Cirstea
Razvan Cirstea
2,493 Points

What is the advantage of using return ?

From the Hangman.java point of view, what is the advantage of using "return isHit" in the Game.java class ? isHit is a public object, can't it be called in Hangman.java without the return method ? Basically, I'm asking how will "return isHit" from the applyGuess method influencing our Hangman.java program ?

2 Answers

Ian Bonyun
Ian Bonyun
4,645 Points

I know this is a super old question, but it's a really good question and it deserves an answer so that other beginners can learn from it.

isHit is not a public object and cannot be called from Hangman.java, nor can it be called from elsewhere in the Game class. You can test this yourself. Go into jshell, open Game.java, create a new game instance, and use the applyGuess() method. Now try to print isHit. It won't work. You get an error: "cannot find symbol" because it doesn't exist anymore. Where did it go? It got deleted (garbage collected?) once applyGuess() finished running because it exists only within the local scope of that method.

That's why it is returned. If you want it to be accessible outside of the method, you need to return it and assign it to a variable in the surrounding scope.

sankalp yadav
sankalp yadav
2,101 Points

consider a world where you are king , there is your kingdom and your queen and all the shit you can think . Imagine a situation comes up in your realm , immediately you train your traveller but also a soldier man to get the timely details of that area where some problem has spiked up.

Now whenever you traveller man send the message through raven to your ear about the real happenings of event in that area .that is only functionality of return. Raven is almighty tool of odin , so he wont die or anybody could catch him ,raven is just invincible.

Had there not been raven , you wont be able to get the exact happening in directly .In that situation your traveller man would tell another stranger to fly up the news as soon as he could, and that stranger would start riding on horse if his horse dies he would have to take another way.

Note :if u are smart-ass you would say what if raven dies .Well thats why told you raven is tool of ODIN .

Now coming to your question why return isHit used although what i am going to tell you wont believe but it is truth lolz, if you print type(isHit) it will say boolean. Returning a True or False is very good design method of programming as when raven takes word to ear of king there is no chance of alteration ,either he will hear true or false nothing else ,which just make program more robust and agile.

i am done bullshitting ,hope you also get you answer if not you can bash me .