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
Kshatriiya .
1,464 PointsWhat is the purpose of "private Game mGame;"?
hi in the video mGame was declared as an object of Game,
private Game mGame;
I generally understand the idea behind it (like doing private String mclass) but what is its purpose and how does it relate to the Game class?
Say for example when you declare "String mName", mName stores a string as a value but when you decalre "Game mGame" what would mGame store?
I really wish the video would go into more details like these things.
Thank you in advance.
2 Answers
aakarshrestha
6,509 Points"String" is a class itself that can store string value in it. Like you pointed out that String mName store a string but what about a class that can hold not only string but also integer. In that case, you have to create a class. Let's say we want to store string and integer in a class so we write a class called Game, just for the sake of understanding the concept.
public class Game {
String mName;
int mAge;
//constructor
public Names(String mName, int mAge) {
this.mName = mName;
this.mAge = mAge;
}
//getter and setter
public String getmName() {
return mName;
}
public void setmName(String mName) {
this.mName = mName;
}
public int getmAge() {
return mAge;
}
public void setmAge(int mAge) {
this.mAge = mAge;
}
}
Now if you want to get both name and age, all you have to do is call the Game class.
Game mGame = new Game("Boy", 12);
Here mGame is the name of the object Game that now holds the values Boy and 12.
I hope it gives you some understanding.
Happy coding!
Kshatriiya .
1,464 PointsThat was a perfect explanation, thank you very much for you help!
aakarshrestha
6,509 PointsIf you look in the folder, there is a file called Game.java. This file has some methods in it. Now in the Prompter class, if you look at the constructor, Game game is the parameter.
Now you create the object of Game class and put it in the parameter of Prompter constructor. It means that the prompter constructor will only take Game object as a parameter.
When you do Game mGame = new Game(); it means that you initialized the object but when you only do Game mGame, it means you assigned a name to Game that is only initialized yet.
Hope i am making it clear.
Happy coding!
Kshatriiya .
1,464 PointsKshatriiya .
1,464 PointsHi, as an extension to the original question, if you could clarify something in the following code for me
As you can see, the only instance mGame is used is to call the applyGuess() method from the Prompter class. In that case why not just make create an instance of mGame using the new method like this as you said above:
Game mGame = new Game();Would that not serve the same purpose as say: