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) Meet Objects Constructors

I dont get it from here.

public PezDispenser(String characterName) { mCharacterName = characterName; }

what does it mean?

3 Answers

Hi there,

As Jeremy said, that piece of code is the constructor. Every class has one and it is run each time an instance of a class is created. It is used to set up the instance (or object, if you prefer) with whatever characteristics are required. In this case, the constructor takes a parameter that's a String. Inside the constructor, the incoming parameter is assigned to the member variable mCharacterName.

There's another post that deals with this, and other, concepts here - that may help you with additional explanations.


That line is your constructor. That is what will run first in that class. It basically takes a string input "characterName" and it sets your instance variable "mCharacterName to the name that was input when the program runs.

thanks :)

No problem! :+1: