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) 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.

Steve.

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: