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 (Retired) Creating the MVP Prompting for Guesses

Lucas Frixione
Lucas Frixione
1,332 Points

I can't seem to understand this constructor...

In the Prompter class he does this

  private Game mGame;
  public Prompter(Game game){

    mGame = game;

  }

why is he passing the new game object as a parameter? can someone explain to me what is going on? and at the end he does this

    return mGame.applyGuess(guess);

which i cant seem to understand either

Tamur Farrokhnia
Tamur Farrokhnia
25,973 Points

You're understanding is correct.

Part of what makes object-oriented programming so powerful is that you can store lots of information in an object, and then use all of it with just one reference to the object.

Just like you can declare a variable of type "int" or class "String", you can declare a variable of class "Game". In this case, every object of class "Prompter" will store an attribute of class "Game" called mGame.

Then, since you need a value for mGame, you include it in your constructor by requiring that you pass in a "Game" object, which you then set as the value of mGame.

It's important to note that you will need to already have an instance of a "Game" object created to pass into the program. So somewhere earlier in the program you would need to use the Game constructor to create a Game object, and then you reference it as an argument in the "Prompter" constructor.

The second bit of your code is just like any other return statement, but it has to run some calculation first. Whenever you run the promptForGuess method, it will take whatever "Game" object you have stored in that particular "Prompter" object, run the "applyGuess" method on it, and return the result.