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 Creating the MVP Prompting for Guesses

hakan Gülay
hakan Gülay
8,489 Points

game

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
STAFF
Craig Dennis
Treehouse Teacher

Hi 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?

hakan Gülay
hakan Gülay
8,489 Points

Hi 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
Craig Dennis
Treehouse Teacher

In Hangman.java we created a new Game object named game.

Game game = new Game("treehouse");

There is a class definition in the Game.java file. See how the new game we created there is of type Game?

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
hakan Gülay
8,489 Points

Thank you so much Craig Dennis I got it now :)

Here'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
Craig Dennis
Treehouse Teacher

:thumbsup: Way to stick with it! Thanks for asking questions!

Ahmad Khaled
Ahmad Khaled
13,026 Points

You sir are a Master :thumbsup: It's just so simple yet so confusing!

Michelle Bramlett
Michelle Bramlett
1,253 Points

This was very helpful to me! Thank you for your input

kemishobowale
kemishobowale
3,627 Points

That's sort of helped me. Might need to watch the video a few more times tho!