Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Java Objects Creating the MVP Prompting for Guesses

faintful
faintful
4,777 Points

What was the point of setting the parameter of the Prompter's constructor to (Game game)?

Craig created the constructor of the Prompter class thusly:

public Prompter (Game game) { this.game = game; }

Don't pay attention to what is inside of the body of the constructor, what I care about and can't explain is why he had to add the (Game game). Later on in the video he creates a Prompter object inside of Hangman.java and it looks like this:

Prompter prompter = new Prompter(game);

And above that line he has this:

Game game = new Game("treehouse");

The way these two lines are interacting with each other ends up being that the Prompter object is essentially telling us that its parameter is "treehouse", correct? So it would essentially look like: Prompter("treehouse");

Did I understand this correctly? And if I did, then what was the point of this? I don't understand why he would do this.

1 Answer

Gabriel Precup
Gabriel Precup
4,565 Points

Not really, no. The object game that is now used in the Prompter has the variable answer equal to “treehouse”, the game object isn’t a String (it’s of Game type) and can’t be equal to a String. As a conclusion, the game you’re playing is based on the word “treehouse”.