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

Grigorij Schleifer
Grigorij Schleifer
10,364 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,364 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 ?

....