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

Noam Elbaz
Noam Elbaz
5,672 Points

Constructor: You used Prompter inside Prompter. Huh?

I don't get it.
Craig constructed "Prompter" inside some class called "Prompter".

Huh?

I get this: Game game = new Game. --> Its a new and unique instance of the Game class. but now Craig is putting one class inside the same class with the very same name.

Confusing.

1 Answer

Aaron Arkie
Aaron Arkie
5,345 Points

Hello, are you talking about this line of code?

public class prompter{ // class name

public prompter(String sForString){

// world changing code.

}

}

Java creates a default constructor for all classes you just cant see it. In order for you to create a constructor you MUST use the name of the class your creating the constructor in. If it accepts a parameter you must pass it something according to its type. Further more constructors are made to initialize member variables we have created in our class.

when you use this line of code: prompter promptME = new prompter(); You are creating an instance of the class and calling a constructor automatically. Constructors are a little confusing but with practice of using methods and constructors you will start to see the difference and the importance of their uses.