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

Jacob Klug
Jacob Klug
469 Points

Why did we do add (String characterName) to public PezDispenser? What does adding that do?

What did ( String characterName) after public Dispenser do for our code?

Adam Bator
Adam Bator
2,327 Points

It requires us to put one String argument when we creating object.

1 Answer

Alex Bratkovskij
Alex Bratkovskij
5,329 Points

Because it is a constructor.

"A constructor is a method that will run when you instantiate the class. Again instantiation happens when you create the new instance of the class using the new keyword. A constructor is a great place to stick required information about the initial set up of the object."

Additionally, this might be usefull: https://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html