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 Prompting for Guesses

Wan Nor Adzahari Wan Tajuddin
Wan Nor Adzahari Wan Tajuddin
2,438 Points

Why do we use boolean isHit in Hangman.java?

I don't understand. What does that line of code do?

boolean isHit = prompter.promptForGuesses();

Something feels wrong for me here. Isn't isHit is a variable that is inside the applyGuess() method in Game.java ?

2 Answers

andren
andren
28,558 Points

Isn't isHit is a variable that is inside the applyGuess() method in Game.java?

It is, but when you call a method it can return data to the code that called it, promptForGuesses returns the result of calling applyGuess, and applyGuess returns the contents of the isHit variable defined within itself.

So applyGuess returns the content of isHit to promptForGuesses which in turn returns it to the method that calls it. In the end the isHit boolean created inside of Hangman.java ends up with the same value that the isHit variable inside of applyGuess stores. But it is technically not the same variable, it is a new variable that just happens to get set to the same value.

Chris Needham
Chris Needham
1,871 Points

In addition to the explanation above, here's another way for you to look at it. Remember when he talked about scope.

Scope is what is in between the curly braces. Because the isHit in hangman is in the scope of the void main method in hangman.java, THAT is Hit is not in scope, it's not visible to this scope.

If you actually wanted to access a value inside class A from class B, the best way to do that would be with getters and setters. You would call a method inside that other class that has access to it's fields.

He will get to that later, I don't want to drag you into the rabbit hole yet.

// The rabbit hole below if you really want to know

If the class is static, you just "dot" into the field, IF that field is public (which we shouldn't do), if it's not you need a getter or setter. If the class isn't static, then it's an instance class and you would create an instance, then "dot" into the class that way to get the field if it's not private. If it's private, then you would set the value by passing in an argument into the public setter method to set the value of the private field in the class.

That's about as short as I can make it... We'll get there, you can change values from other classes like that but point blank, that variable isn't in scope and, because it's not an object field, there's really no way to bring it in scope directly anyway, but if you wanted to, above is how.

Think of it this way: by default, you protect the fields of the object. Then , you can allow "read" access if you create getters and "write" access to those values if you create setters. This principle is called encapsulation or "data hiding".

Encapsulation is one of the 4 tenets of OOP. I'll write these 4 below. I use the acronym A-PIE. You might think of the saying "easy as pie" to help remember. A. Abstraction (Abstract classes and interfaces) - neither of these are stand alone implementations, both are implemented by way of inheritance. P. Polymorphism, means "many forms" (Anything that causes an object to pass more than one "Is a" tests.) I. Inheritance (Happens anytime you use the word extends or implements) E. Encapsulation (The data hiding principle just mentioned). When you hear encapsulation, think data protection or hiding using getters and setters (read and write capable object methods).