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

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Why i have to store game in mGame?

I don´t understand the logic behind building the constructor "Prompter" and the need of building a member variable "mGame" to store the game logic in it ....

I just don´t want to loose my mind :)

public class Prompter {
  private Game mGame;  


  public Prompter (Game game) {
      mGame = game;
    }
Demauri Portis
Demauri Portis
4,809 Points

Your mGame has an access modifier of private, meaning the field is accessible only within its own class. You can gain access to that variable indirectly by using public methods like the one you have Prompter. So basically, your Prompter method is a set method (set the value of mGame) allowing you to initialize mGame through a different class which would extend Prompter.

~Hope it made sense

Matthew Turner
Matthew Turner
2,564 Points

The idea behind making classes is so you can make many instances of a class. The Prompter constructor takes one parameter which is a Game object. The Game object that you pass to the Prompter is saved to the member variable in the class. This is useful for when or if you want to create multiple instances. Hope this is helpful.

2 Answers

Tyler Chapman
Tyler Chapman
20,470 Points

You need to use the this keyword

public class Prompter {
  private Game mGame;  


  public Prompter (Game game) {
      this.mGame = game;
    }
Grigorij Schleifer
Grigorij Schleifer
10,365 Points

But why i have to set private to the mGame??? Why i can´t code

public Game mGame?

Why do we need mGame and game??? Why you can´t access mGame from other classes ?

....