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 trialTaebin You
4,786 Pointsprivate 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
Full Stack JavaScript Techdegree Graduate 29,228 Pointsprivate 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
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
Full Stack JavaScript Techdegree Graduate 29,228 PointsIt'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.
Timothy Hilley
2,292 Pointsi 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.
Araz kuberin
6,199 Pointsi also wonder How is it different from Game game = new Game()??
Patryk Moros
819 PointsI 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
8,535 PointsIt's a constructor. It creates an instance of the game class.
Taebin You
4,786 PointsHow is it different from Game game = new Game()??