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 trialBjorn Van de Weyngaert
1,297 PointsPezDispenser question
Question 1 In the following code:
String before = dispenser.swapHead("Darth Vader");
System.out.print("It was %s but Chris switched it to %s %n", before, dispenser.getCharacterName());
What would be the difference if
String before = dispenser.swapHead("Darth Vader");
Got changed into:
String before = PezDispenser.swapHead("Darth Vader");
Would the program still be able to call on the swapHead method in PezDispenser.java?
Question 2 After running this code (assuming we didn't add final and if we would actually want to be able to change the characterName), shouldn't the compiler create a new .class file to store the "Darth Vader" class?
PezDispenser dispenser = new PezDispenser("Darth Vader");
Thank you for your help!
2 Answers
Jian Chen
4,938 PointsHey Bjorn,
for your first question, calling |PezDispenser.swapHead("Darth Vader")| directly like this would imply that you're calling .swapHead as a static method (basically asking for the property of the class itself). The class should not/cannnot take methods to modify the class because the job of your PezDispenser class is to act as a blueprint to build pezdispensers on demand. It is the newly made pezdispenser (also an instance) that should act like an actual pezdispenser, not the class itself. So when you call |dispenser.swapHead("Darth Vader")|, you are performing the .swapHead on the actual pezdispenser (instance) that you probably made with this line |PezDispenser dispenser = new PezDispenser()| naming this pezdispense (instance) as "dispenser" in the constructor as you can see. Key thing is to keep in mind is that the class is the blueprint for building the actual pezdispenser as an instance that you can interactive within code like an actual tangible thing; Call the method that suits the purpose of an object: on a class (PezDispenser) you should only call method to do things such as constructing an instance or asking about a property of the class, while an instance of the class (dispenser) can do things like .swapHead or .loadPez. This way you can build multiple instances that all have a different head, instead of limiting yourself to just having one pezdispenser as a class, whose only job is to be the blueprint for these instances. I think this should clarify question 2 as well.
Short Rephrase: class is a static variable and instance is a non-static variable. you can only call static methods (like .MAXPEZ) on a static variable (like the class: PezDispenser). Non-static methods (like .swapHead()) can only be call on a non-static variable (like the instance: dispenser). Notice how the static method does not take parameters(). The line |PezDispenser.swapHead("Darth Vader")| would simply not compile because you cannot call a non-static method on a static variable. There a reason why there is the sperapration of static and non-static: once you import the class, think of being static like being always there with unchangable properties (because you should use the declaration, Final) . Non-static vaiable needs first to be created (with instantiation of a static variable/class) in order to exist; unlike a static variable, you can also create copies of a non-static variable just with a constuctor method (of the static variable). An instance is basically a non-static version/copy of the static variable/class.
Gabe Olesen
1,606 PointsHello Bjorn!
If I'm correct, it would't compile because PezDispenser is the class. You need to instantiate an object/instance(dispenser) for you to call the methods in the PezDispenser class.
In other words;
dispenser is an instance of class PezDispenser thus when you do dispenser.swapHead, you're calling the swapHead method using the instance dispenser from the PezDispenser class.
Question Two:
If you remember in the video Craig explained the following summarised;
When you enter an argument using () as shown below:
class PezDispenser {
final private String characterName;
public PezDispenser(String characterName){
this.characterName = characterName;
}
}
A user enters in a name i.e. "Darth Vadar" this is not a class. The user choose what head they wanted on their Pez Dispenser i.e. "Darth Vadar" it's then stored in variable characterName.
When we refer to this constructor (shown above), we pass through an argument (String characterName). We've declared the variable private and final meaning; private = only accessible within that class and final = only be initialised once.
So when we create instance on the Example.java:
PezDispenser dispenser = new PezDispenser("Darth Vadar");
We're assigning "Darth Vadar" to variable characterName.
We also achieve encapsulation which we'll all learn soon!
~Gabe
Bjorn Van de Weyngaert
1,297 PointsThank you for the reply, your answer in combination with the answer of Jian Chen clarified things :-).
Thanks a lot!