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

Brad Fowler
Brad Fowler
479 Points

Just to clarify...

I'm still trying to wrap my head around all of these lines of code, their names and functions...any comments correcting or confirming would be great!

So, we have the class: class PezDispenser { //Is this kind of like a descriptive name at the top of an outline, and said outline holds the objects attributes??

Then our private method: private String charcterName = "yoda"; //And we can add a list of private attributes here, then just make specific items available using constructors?

Then our constructor: public String getCharacterName(){ //The "getter" that allows the Sysout statement to GET access to the attribute?

Then, the data allowed by the constructor: return characterName; //Am I even close? Really trying to comprehend what things do what, and I need to understand them in my own terms sometimes.

Thanks in advance for any clarification!

1 Answer

Ben Reynolds
Ben Reynolds
35,170 Points
// Here we name the class, and the code we put inside will be a "blueprint" for how we want
// each instance of the class to work
class PezDispenser {  

     // This is a private field inside the class. We don't want it to be changed,
     // but it should still be readable. That where the "getter" comes in (at the end of the class)
     private String characterName;

     // This is the constructor. You can identify it by its name which is always exactly
     // the same as the class name and it doesn't have a return type. When you make an instance
     // of the class with the "new" keyword, like PezDispenser yoda = new PezDispenser("Yoda"),
     // we're calling the constructor and passing it a value for the name

     public PezDispenser(String name) {
          // Inside the constructor, it takes the name we pass in and uses it to set the characterName field.
          characterName = name; 
     }

     // This is the "getter" method mentioned earlier. It's just a regular method whose purpose is to
     // let the characterName field be retrieved. Since characterName is private, it can't be accessed from
     // outside the class, so this public method provides a way to get the name from elsewhere in the code.
     // There's no "setter" here so the field is read-only from outside the class
     public String getCharacterName() {
          return characterName
     }
}
Brad Fowler
Brad Fowler
479 Points

Ahh...so, I'm totally wrong. Awesome.

Thanks, Ben. I'll get my head around this eventually!