Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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.