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

Taebin You
Taebin You
4,786 Points

private Game game

I am just confused what private Game game is. I know that Game is the Game class and game is just declared variable

But what does it actually do?

7 Answers

Raphaël Seguin
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Raphaël Seguin
Full Stack JavaScript Techdegree Graduate 29,228 Points

private Game game; //declares the object of type Game which reserve some memory

Game game = new Game(); //instantiates it by calling the constructor Game() which effectively creates the object with its own fields and methods.

Raphaël Seguin
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Raphaël Seguin
Full Stack JavaScript Techdegree Graduate 29,228 Points

"private Game game" means you're declaring a private object of the class Game that is called game.

...Then you'll need to instantiate it in order to make it exist as a real object.

Raphaël Seguin
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Raphaël Seguin
Full Stack JavaScript Techdegree Graduate 29,228 Points

It's just breaking the declaration and instantiation in two different statements. Sometimes it's better to declare an object at the top of your code and then instantiate it when you need it. You can also do both at once.

i think youre missing a little vocab, and i just got it myself. pros out there reading this please correct me if im wrong:

Class Instance = new Constructor("input") Class is the file that you are in with methods to call upon anywhere within the folder, instance is the title of the one specific object you are making right now, theres always a blueprint to construct, but your making the "game" game this time. theres a materials (the Class) but game is the name of what you are building (constructing). the method Game(), or the constructor, is simply a method in the Game class being called on to attatch the input to the object "game".

private Game game; in Class Prompter is to tell people non verbally that Class Game Instance Game is ready, like we had to make: public String answer; for => Game(String answer) to connect "treehouse" to the game. We have to connect the game to the prompter in the same fashion.

i also wonder How is it different from Game game = new Game()??

Patryk Moros
Patryk Moros
819 Points

I think that:

private Game game;

is a declaration of already existing instance of Game class in Hangman.java:

Game game = new Game("treehouse");

but correct me if I'm wrong

Jazz Jones
Jazz Jones
8,535 Points

It's a constructor. It creates an instance of the game class.

Taebin You
Taebin You
4,786 Points

How is it different from Game game = new Game()??