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
Unsubscribed User
1,785 PointsWhat does that mean: public Prompter (Game game) { } ?
Hey there! I've been trying to understand this part of code for so long but I still cannot really figure out what it means and what it does. I know it's a lot of questions but I would really appreciate if you could help me:
class Prompter {
private Game game;
//THIS PART HERE!
public Prompter(Game game) {
this.game = game;
}
I know that:
private Game game;
declares the object of Game.java to save memory but here are my first two questions:
- Why is it priavte?
- We declare it so we can use the game object in Prompter.java, right? Why don't we just do this, like usual?:
Game game = new Game();
Now, I also don't get what the second part (where the comment is) does. Does every class need a constructor or why do we have one here? What does this constructor do? I know it takes a type "Game" but where do we call that Prompter object and plug "Game" in? And what even is the type "Game"? I mean, I know what a string type is, but what is the type "Game"?
And finally, what are we setting the game object, that we declared in:
private Game game;
equal to in this part?:
this.game = game;
Hope you can help me :) Julian
4 Answers
Philip Gales
15,193 PointsGreat questions!
Why is it private?
We declare field variables private so other classes cannot change them. We instead allow other classes/objects to use getter methods to change them in ways we intended. This practice is called encapsulation.
If you take the Java objects course, you will work with this concept a lot.
We declare it so we can use the game object in Prompter.java, right? Why don't we just do this, like usual?:
They are the same. Well., sort of.
Game game = new Game();
/* This creates a variable game of type Game.
We then set that variable equal to a new Game.
*/
public Game game;
game = new Game();
/* This is the same as above, written on 2 lines*/
/******************************************************************/
private Game game;
/* This is the same as above, but with our game
variable set to private. And we never set the game variable
as a new Game object.
*/
this.game = game;
/* This sets that game object we created previously to
a Game object. This is the same as the 'written on 2 lines'
way I did above, but with code in between the lines.
I will explain why we use 'this' later.
*/
Does every class need a constructor or why do we have one here? What does this constructor do? I know it takes a type "Game" but where do we call that Prompter object and plug "Game" in? And what even is the type "Game"? I mean, I know what a string type is, but what is the type "Game"?
Every class has a default constructor that has no parameters. You don't see it, but 'public Prompter() {}' exists and is hidden.
By creating our own with 'Game' as a parameter, we are able to then use that 'Game' object that was passed in. This is great for passing objects around. This is the same as having the object 'String' as a parameter. String and Game are both classes and objects in this project. String just happens to come default and the class is hidden.
Passing in the custom object Game is helpful because you are able to manipulate the same Game object that another class started. This would be like saying I want to come home to the person object myMom and not a different person object someOtherMom. By passing in game, you are able to keep working with the same object. And instead of it being a person object, it happens to be a Game object.
And finally, what are we setting the game object, that we declared in:
When 2 variables have the same identifier (name), we use 'this' to refer to the class (field) variable. When you learned about scope, you learned that a variable can exist within a class, or within a specific method. In this case, we happen to have a class variable named game, and a variable that exists within a method named game. 'this.game' tells the compiler that we are referring to the class variable.
class Prompter {
private Game game; //<---class variable
public Prompter(Game game) {
this.game = game; //<-----this.game is the class variable
}
you could also rewrite the above as:
class Prompter {
private Game classGame; //<---class variable
public Prompter(Game game) {
classGame = game; //<-----classGame is the class variable
}
I hope this all helped some!
Philip Gales
15,193 PointsI am so happy you seem to be understanding the concept of Object Orient Programming (OOP)! With that said, I will try to answer your questions, but without all of the code, some of my answers may seem off. I will also pretend like you are working for a company writing this code, as it will help me explain some of the reasons.
1: My old question: Why is it private? Everything important, including Strings that hold the answer, hits, and misses, are already set to private
I assume when you say everything important, you are referring to everything in the Game class.
When you finish this project, and the next person joins to fix some bugs or add upgrades, they may mistakenly access the game object in the prompter class and use that to change the game object. In other words, they may call setter methods on your game object using your prompter class. When your prompter class returns the game object to Hangman, the game object may have been changed in ways not intended.
Let say I am making this hangman game, and I use Game.java to store the word that people guess. Hangman.java then creates a new game and uses the setter method to set the word to "Apples". Hangman.java then passes this game object to Prompter.java to prompt for guesses.
By setting that game object in Promter.java to private, no one is able to change the word "Apples" while in the control of Prompter. Hangman.java may have a way to change the word, but Prompter is currently in possession of the object and you don't want others changing the word.
If Game was set to public in Prompter, some random class like Stranger.java can accidently access the game object in Prompter.java and call the setter method to change the word. If the object was private, they would only have been able to access public methods available in Prompter.
Another way to say this is, imagine I have a phone. I can access the phone and change my wallpaper, but when I give you my phone, I lock it. You now have possession of my phone to clean it or change the case, but you can't access parts of it that I don't want you to. You might be careless with my phone and allow others to access it, but it has already been locked. If I didn't lock my phone when I gave it to you, someone may have taken the phone from you and changed parts of it that I didn't want them to.
Doesn’t that also mean, that whenever we create an object somewhere and we change it’s values, the "blueprint" class is never really changed, just the temporarily object that was created?
Correct. This is the point of OOP. To create blueprints and use them to create X amount of objects, with only a blueprint worth of code. Each object will take more space to save, but the code required doesn't change.
Is setting our already declared “game” instance now to the already used object in Hangman.java?
Yes. We are hoping Hangman.java setup the game object how it liked and passes that game to Prompter.java to prompt for guesses.
Basically, that means we don’t really plug in a type (like String) in ANY constructor, we always plug in an object, so a certain code, that the class can use then?
Yes. The point of parameters is so each method can work with an object. This makes your code reusable to work with other objects of the same type without rewriting code.
I highly recommend you search "OOP explained" on YouTube or Google. Understanding objects will help everything else in Java make sense.
Unsubscribed User
1,785 PointsThanks a lot Philip!
I got everything except for one thing.^^
So when we set the game object in Prompter.java to "private", does that mean NO OTHER CLASS could now access this object? Like could no other class access anything of that object, not even public methods, strings etc.?
What I meant with "everything important is already set to private" were the Strings that contained the misses, hits, and the answer. Since they were already private, I was just confused why we set the game object to private again.
So basically, the only reason why we set the game object to private, was to prevent people and other classes from accessing and potentially changing the object by creating a setter? I mean if we set the object to public, everyone could create a setter and change even private fields, right? This way, no one could even access them; therefore, they could also not change them.
Hope I got it.
Philip Gales
15,193 PointsWe set it to private to force other classes to use the methods we create in the prompter class. The public methods will allow other classes to use the game object how we want them to, without any potential for them to change the object in unintended ways.
If the game object was public, other classes could access it directly and then call setter methods from Game.java. Since it is private, they can't even view the game object and can only work with public methods in Prompter.
Soumitra Mehrotra
1,468 PointsBut why do the Prompter.java needs to access the object of Game.java, as Prompter.java is not using any field or method of Game.java?
Unsubscribed User
1,785 PointsUnsubscribed User
1,785 PointsHey Philip Gales!
Thank you so much for your detailed answer! Your answer helped a lot but you might have misunderstood some of my questions. This reply will definitely be long due to lots of code but it is really not that hard^^
private Game game;I know what private does, I just don’t get why we set it to private. I mean what could other classes potentially access or change? Everything important, including Strings that hold the answer, hits, and misses, are already set to private. Why don’t we just set it to public? Even then, no other class could change our object and they could only run methods on it.
Just so I got you right: You are saying that this:
Game game = new Game();Is the same as this?:
Wouldn’t that mean, that this:
= new Game();has the same function as this but the only difference is that it doesn’t create a new object, it just assigns the old one from Hangman.java?:
Thanks!!!! This helped me a lot!!! :))
So you mean, we do this:
so that Prompter.java can now use the same object that Hangman.java is using? Does that mean that if any changes were made to the object that Hangman.java is using, Prompter.java could access and work with these changes too? Like let’s say there was a String in Game.java (the class) that had an original value of “Hello”. If Hangman.java now changed the value of the String in its own object to “World”, Prompter.java would now access the value “World” because it accesses the same object as Hangman.java?
So, you also say that if Prompter.java did this: Game game = new Game(); , it would create a completely new object just like it is declared in the "blueprint" class, and it would never be able to access the changes that were made to the object that Hangman.java uses, and it could only access changes that were made to its own object by its own class (Prompter.java)?
Doesn’t that also mean, that whenever we create an object somewhere and we change it’s values, the "blueprint" class is never really changed, just the temporarily object that was created?
Does that mean, all we do in this line:
this.game = game;Is setting our already declared “game” instance now to the already used object in Hangman.java?
was initializing the object that was already used by Hangman.java so Prompter.java could use it? And then, we just set it to private?
Basically, that means we don’t really plug in a type (like String) in ANY constructor, we always plug in an object, so a certain code, that the class can use then?
Sorry for the long reply but I think I got it due to you! :)