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

Vincent Murray
Vincent Murray
11,300 Points

private Game mGame

Hi

I have missed something here , I don't understand the line

private Game mGame;

could someone please explain it to me

2 Answers

Lucas Santos
Lucas Santos
19,315 Points

This one gave me problems when I first learned it as well so i'll try my best here.

private Game mGame;

What you are doing here is initializing a Game object named mGame

So you know how every time we define a variable we have to also define it's type like so:

public String name = "John";

So as you see I defined the type of object before the variable name which in this case is String.

We basically did the same thing with mGame.

So Game is the type and mGame is the variable name, and all we did was initialize it without a value of any sort.

So again comparing this to my example just to be clear,

name is a type of String object

public String name = "John";

mGame is a type of Game object

public Game mGame;
Vincent Murray
Vincent Murray
11,300 Points

Thank you ,much appreciated

thanks lucus...

kabir k
PLUS
kabir k
Courses Plus Student 18,036 Points

The line

private Game mGame;

in

public class Prompter { 
  private Game mGame; 

  public Prompter(Game game) { 
    mGame = game; 
  }

}

means a private member variable for games, where private is an access modifier that shows we are protecting our data by hiding it. Game is the class for game object (that's going to be stored in mGame) and serves as the class data type for our member variable, mGame which is going to store the game object passed to the Prompter constructor so that upon the creation of a new prompter object, the game object is passed to it.

In other words, that line is needed so that whenever you create an instance of the Prompter class (i.e. a new prompter), you'll have to pass it a game object which is then stored the private variable, mGame as initialized in the Prompter constructor above.

Hope that helps.

Matthew Philip Uy
Matthew Philip Uy
3,615 Points

if I change it from public Game game; to public String game; what will happen?