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 trialVincent Murray
11,300 Pointsprivate 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
19,315 PointsThis 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;
kabir k
Courses Plus Student 18,036 PointsThe 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
3,615 Pointsif I change it from public Game game; to public String game; what will happen?
Vincent Murray
11,300 PointsVincent Murray
11,300 PointsThank you ,much appreciated
Anoop Kumar
1,715 PointsAnoop Kumar
1,715 Pointsthanks lucus...