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


Trying to understand what each line does by breaking down the code

I just wanted to better understand what each individual line does, as i believe thoroughly knowing the pieces will help understand the whole. Please correct me if I'm wrong. For the Pez.java file: the public class PezDispenser basically assigns the file name & the private String mCharacterName makes the object a string, which is why we get back the name of the character instead of say a circle, or square. -public PezDispenser(String characterName), creates a new string variable called characterName & (the next line) makes it equal to the value of mCharacterName allowing us to change the private method. -public String getCharacterName() is a command that will return the name of the string of characterName.

For the example.java file: PezDispenser dispenser = new Pezdispenser (); simply creates the object from the blueprints (aka PezDispenser.java).

-Lastly System.out.printf(.........%s \n", dispenser.getCharacterName()); prints out the name

What i don't understand is why we physically have to input a name for new Pezdispenser and how his "Donatello" becomes the character's name

1 Answer

Rebecca Bompiani
Rebecca Bompiani
16,948 Points

Hi David-

Great question!

Let's run through the code, first:

public class PezDispenser{                  //creates a new class, PezDispenser
  private String mCharacterName;      // creates a member variable for the class, which stores the character name

  public PezDispenser(String characterName){      //this is a constructor method for the class, 
                                                                                  //used to create a new PezDispenser object. It takes an argument,
                                                                                 //characterName, which is chose by the user. There can be 
                                                                                  //more than one PezDispenser object at any given time, hence 
                                                                                  //the user can put in any name he/she wants

     mCharacterName = characterName;       //this takes the mCharacter name, a property that EVERY 
                                                                        //member (object) of the PezDispenser class has, and sets 
                                                                        //it to the characterName the user passed in when creating the object

  public String getCharacterName(){     //a method for the PezDispenser class
    return mCharacterName;                    //returns the otherwise private mCharacterName

Basically, you create classes, which are a blueprint for all new objects that will be created. Say you want to make a series of pez dispensers. They will all have a name, though those names may be different. So for the class, we'll set up a member variable for the name and leave it blank. We'll also set up a way to retrieve that name.

For each different TYPE of pez dispenser, we'll want to create an object from that blueprint (class). Each object will inherit all of the methods and variables set forth in the class. To create new objects from the class, we need a constructor method. The constructor method lets the user select a name for the pezDispenser object as it's created, and sets the name variable it inherited from the class to match that chosen name.

I remember first starting out with objects and classes, and was certainly confused! Over time using and creating objects, it'll all start to make a little more sense. Hopefully that explanation helped a little!