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!

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 Final

Sumit Doshi
Sumit Doshi
388 Points

Please help me its quite confusing!!!!!!

("It was %s but chris switched it to %s",before,dispenser.getCharacterName);

So, we are using two %s here.. %s - Yoda - dispenser.getCharacterName

%s - Darth Vader - before

My Question is HOW did YODA come first@(It was %s) when before is used in the statement and then comes dispenser.getCharacterName...

Is it not supposed to be "It was Darth vader but chris switched it to Yoda"

OUTPUT It was Yoda but chris switched it to Darth Vader

5 Answers

Yanuar Prakoso
Yanuar Prakoso
15,196 Points

Hi Sumit The main reason why YODA comes first is presented in the swapHead(String characterName) method in PezDispenser class:

public String swapHead(String characterName) {
String originalCharacterName = this.characterName; //in here originalCaracterName = this.characterName = "Yoda"
this.characterName = characterName; //in here this.characterName = characterName = "Darth Vader"
return originalCharacterName; //originalCaracterName ="Yoda" returned or passed to String before = dispenser.swapHead("Darth Vader");

I put some comment pertaining the value of each variable in each code line. If you already get the reasoning by using that comments great. If you still curious here is the long boring explanation. I apologize for it but here we go:

The first thing you must remember is this.characterName is NOT the same variable as characterName. this.characterName is the variable declared first when you instantiate (let us just call this make new PezDispenser) you put "Yoda" right?

PezDispenser dispenser = new PezDispenser("Yoda");

I hope this is what you wrote in your code. Meaning before it was changed the head will be Yoda. Then Chris activated the swapHead code written above right. This is where the things got a little bit tricky. In order to swap head Chris must use this:

String before = dispenser.swapHead("Darth Vader");

Question is what is the value of before after it passed the code? Here is the answer to that: At the beginning of Chris put the characterName = "Darth vader" inside the swapHead method.

However, inside the PezDispenser class which the swapHead method resides there was already variable called characterName right? Well to differentiate this Java allows the developer to use this.characterName to designate the original variable in the class and characterName as variable that Chris just inputted.

Thus inside the swapHead method first we transfer the value of this.characterName which is Yoda to String variable called originalCaracterName. Here you need to remember that now originalCharacterName = 'Yoda'!

Then the method change the this.characterName to the characterName input from Chris which is 'Darth Vader' so from now on if you call the dispenser.getCharacterName() it will give you 'Darth Vader' because the initial characterName variable in the PezDispenser class is chaged by Chris by using:

this.characterName = characterName;

as I said this.characterName and characterName is two different things.

But what you must pay attention is in the Example class variable String before called for dispenser.swapHead right? Like this:

String before = dispenser.swapHead("Darth Vader");

So please pay attention that dispenser.swapHead method returns.... the originalCharacterName which stored value of "Yoda" since it was passed from this.characterName befor the this.characterName value was being altered right?

Therefore in the code:

System.out.printf("It was %s but Chris switched to %s", before, dispenser.getCharacterName);

the first %s will = before means = "Yoda"

the second %s will = dispenser.getCharacterName = "Darth Vader" since Chris already change this.characterName = characterName = "Darth Vader" NOTE after this Chris use swapHead whenever you use dispenser.getCharacterName it will return Darth Vader rather than Yoda.

I am sorry for my long explaination. If you get the message from few lines of my answer good you do not need to read all the way to the bottom. I hope this can help you a little.

Jason Anders
Jason Anders
Treehouse Moderator 145,855 Points

Yanuar Prakoso => Excellent answer! Very well explained and detailed. Thank you for helping in the Community! :thumbsup: :smiley:


Chad Lewis
Chad Lewis
1,191 Points

Yanuar Prakoso, well done! I was really confused on how that method operated on the 'String before' variable, your very clear and in-depth explanation made me understand completely. Thank you so much for your contribution!

Zachary Martin
Zachary Martin
3,545 Points

I'm still slightly confused because my thinking is that since the before string is set to the swapHead method my thinking is that it would skip Yoda and be set straight to DarthVader because thats whats inside of the parameters.

I think there's some order of precedence that i'm unclear about because I don't understand how Yoda gets in here at all.

Urooba Urooba
Urooba Urooba
3,565 Points

thank you so much for making it easy for us newbies Yanuar Prakoso.

Excellent answer by Yanuar Prakoso, extremely helpful to me! Thank you!

so if I understood correctly, when we write originalCharacterName= this.characterName, what we actually do is transfer the value from this.characterName to originalCharacterName, so now that this.characterName is freed up, we could give it a new value, which is characterName?