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

Tetsuya Uno
Tetsuya Uno
21,024 Points

What is the meaning of public Prompter(Game game) {this.game = game;}

Why is he creating this as a constructor? Does this need to be a constructor?

Also why does he need to create "this.game = game"?

1 Answer

Brice Roberts
Brice Roberts
22,415 Points

Yes, you will need the constructor.

The keyword this is used to instantiate the class reference to the instance of the class that you are currently working with.

In this instance, the game class is a high level set of rules that define what a game is.

"This" is used to tell the compiler that you are referencing this instance, and not the static instance of game.

Check out these two links. They have pretty good explanations.

https://teamtreehouse.com/community/using-the-this-keyword

https://stackoverflow.com/questions/2411270/when-should-i-use-this-in-a-class