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

Chaz Hall
Chaz Hall
1,970 Points

Scrabble- Why does he name the game, game?

I don't understand why the instructor names the constructor game game. That is very confusing to me. Please explain. Thanks.

2 Answers

He didn't name the constructor game game. The name of the constructor is Prompter, the same as the class name. He is creating the constructor to take a game object as an argument. To create a formal parameter or argument for the constructor he needs both a type and a name. The type is Game, and the name of the parameter is game. He then creates a private member (instance) variable named mGame of type Game to store the passed in game object in.

What this all adds up to is that when you create a Prompter object you will need to pass it a game.

E.g.,

Game g = new Game();
Prompter p = new Prompter(g);

As he says: "So now if you go to create a prompter, you'll give it a game object. And we will store that in a private variable called mGame."

Michelle Miller
Michelle Miller
610 Points

So is my understanding here correct?

He has created a Game class in the Game.java file.

He is using that class to create a Game object within the Prompter class.

So, Prompter (TYPE=Game, NAME=[string]) to create a new prompter object.

Am I understanding that correctly?

Yes, the file name for a class must be the same as the class. So for a Game class it's Game.java

Not quite: he uses that class to create a Game object in the Hangman class:

Game game = new Game("treehouse");

In the Prompter class he writes a constructor that takes a game as input (as it's formal parameter):

public Prompter(Game game) {
    mGame = game;
}

In the Hangman class he uses this constructor to create a Prompter object named prompter:

Prompter prompter = new Prompter(game);

The game object he passes to the Prompter constructor in this statement is the one created above: Game game = new Game("treehouse");

Hope this helps.