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

I am not getting this java code

Hi everyone, I am struggling to understand what the code below does in the java course on objects. If could explain it to me, it would be greatly appreciated, thanks

System.out.println("We are a making a new pEz dispenser"); PezDispenser dispenser = new PezDispenser("yoad"); System.out.printf("the dispenser character is %s\n", dispenser mCharacterName()); } }

3 Answers

Hi Aaron,

i format you code first:

System.out.println("We are a making a new pEz dispenser");

//System is a class inside Java, its upper cased and classes are upper cased
//then you see a (.) character, you use this to access a method of the System class
//then (.) again and here you access the println method of the System class
//so the Java language has methodes inside classes like System for you, you don´t need to write them all

PezDispenser dispenser = new PezDispenser("yoad"); 

//this one is tricky
//here you create a PezDispenser object and call is dispenser
//you have a class PezDispenser and you can make an instance of that class somewhere in your code
//the instance of the class is the same as an object
//so "dispenser" is an object of PezDispenser class
//a class is a blueprint where you define what an object knows (variables) and can do (methods)
//the new PezDispenser("yoad") line is also nice
//The new keyword is a Java operator that creates the object, it called instantiation = creating an instance/object
// and then : Initialization: The new operator is followed by a call to a constructor, which initializes the new object
//the second PezDispenser is the constructor name
//this is the reason why constructor and class share the same name
//if you look at your constructor, you see a String argument inside the () of the constructor
//so you put your name inside of it ("yoad")


System.out.printf("the dispenser character is %s\n", dispenser.mCharacterName); 

  //one method of the System class  is printf 
  //here you open the parenthesis and whrite your text inside " "
  //%s is a String representation, where you put your %s, there will be your String after compilation
  //so it looks like this ("bla bla %s ", your String variable); , dont forget the (,) and name of your String variable after the ""
 //to use the variables of the PexDispenser object call the object name dispenser and  use (.) to access the member 
// variable mCharacterName
//so dispenser.mCharacterName will return a String representation of mCharacterName 

I hope it helps

Grigorij

The first line simply prints We are a making a new pEz dispenser. The second is simply creating a new pez dispenser object and passing a string to its constructor. Then the last gets the name from the newly constructed dispenser object. So for example you would say AronBanerJee aron = new AronBanerJee("Aron"). Then later if you need to know Arons name you would simply call aron.mCharacterName; Although generally the object will have a setter function for this variable. Hope this helps ! =D your friendly mod Doug.

System.out.println("random text"); prints the text and then automatically adds \n to the end of the line.

System.out.printf("random formatted text"); this statement prints out a formatted line, for example you wanted to print out a variable in your text you would add %s in your text System.out.printf("you have %s cookies.", numberOfCookies); this would print out "you have 10 cookies."

I hope this helps as well.