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 trialhakan Gülay
8,489 Pointsgame
I couldn't understand what is going here. I am so confused. What is private Game game ?
For example, we use like this ; private String game. But we used Game here instead of String(I don't mean that we have to use String. It is just example.)
and also public Prompter (Game game) it is same like what I said above. Can you explain step by step please ?
private Game game;
public Prompter(Game game)
{
}
3 Answers
Craig Dennis
Treehouse TeacherHi hakan Gülay !
Game
is a data type that we created by creating the Game
class. It currently is storing the answer. We are going to use this to store the logic of hits and misses. Just like how you can pass a String
object around, you can do the same with the Game
instance that we created in Hangman.java
.
Does that help make sense?
codebyamir
12,108 PointsHere's the format when declaring variables in Java:
<access modifier> <data type> <variable name>
So when we want to declare a private String named game, we write:
private String game;
When we create a class named Game, that represents a new data type. So we can create an instance of the class Game using:
private Game game;
Once created, we can invoke the class methods of Game using:
game.methodName();
Craig Dennis
Treehouse TeacherWay to stick with it! Thanks for asking questions!
Ahmad Khaled
13,026 PointsYou sir are a Master It's just so simple yet so confusing!
Michelle Bramlett
1,253 PointsThis was very helpful to me! Thank you for your input
kemishobowale
3,627 PointsThat's sort of helped me. Might need to watch the video a few more times tho!
hakan Gülay
8,489 Pointshakan Gülay
8,489 PointsHi Craig Dennis
I am sorry that I couldn't understand :/ I understood everthing clearly until here but I'm stuck here.
That Game is data type and also Class which you created ? and other game is just variable ? I don't know what I should say but I still can't understand
Craig Dennis
Treehouse TeacherCraig Dennis
Treehouse TeacherIn
Hangman.java
we created a newGame
object namedgame
.Game game = new Game("treehouse");
There is a class definition in the
Game.java
file. See how the newgame
we created there is of typeGame
?We then pass that into the prompter object and store that in a field. The type is just like we have been doing for String, we need to specify it's type.
That help?
hakan Gülay
8,489 Pointshakan Gülay
8,489 PointsThank you so much Craig Dennis I got it now :)