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 Access Level Modifiers

Bit confused - Need Help

This is the code:

System.out.println("We are making a new PEZ Dispenser"); PezDispenser dispenser = new PezDispenser(); System.out.printf("The dispenser is %s %n", dispenser.characterName);

I don't understand why I have to add System.out.println. What does it do?

Another question is, why do we have to state PezDispenser as new?

Ernest Umeh,

I interpret your first question as : Why do we need to use println in contrast to printf. Simply put, You use the println because you want to print that text to the screen. Next you use the printf method, because you are formatting a string. You pass in a object variable in the formatter. It formats the input and uses references the dispenser.characterName and prints the end result to the screen.

As for your second question: . You want to use the charactername of a Pezdispenser. But Pezdispenser is a class. The blueprint. In order to make an instance of it, you use the keyword "new ((classname))". Now, in your code you created an object named dispenser based on the PezDispenser class ( the blueprint ). According to the blueprint, it has a charactername automatically. So in order to use that objects charactername, you put that in the printf function. the print f works as i described below.

Hope this helps, if i need to explain some more, just tag me :)

2 Answers

OK I'm getting in now. Thanks a lot for the info! Really do appriciate it.

anytime!

A constructor is the main attributes of the object that creates an object from the template. Look at a class as a play-dough mold, and the variables it takes in is the play-dough, creating an object (the instance). Without the constructor, instances cant be made. The class itself is not an object, but a blueprint on making that object. the instances are the objects. So in other classes, i can make as many playdough molds as i please and use them separately as seperate objects of the same class, because of the constructor.

public class PlayDoughStar {

private String starColor;

public PlayDoughStar(String starColor) {
     this.starColor= starColor;
} 
//the rest of the class code
}

so in a different class, i want to make a play-dough star:

public class Table {
PlayDoughStar redStar = new PlaydoughStar("red");
PlayDoughStar blueStar = new PlaydoughStar("blue");
//Tables constructor here {
//}

and use println if there are no %calls , if so printf(ormat)

I like that example.