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 (Retired) Meet Objects Constructors

Curtis Watson
Curtis Watson
396 Points

Conceptual Issue - What's the point?

I'm having difficulty understanding the actual reasons for constructing a class in the first place. Though I understand the radio analogy, that's not particularly applicable in this instance because you can see in Example.java

 PezDispenser dispenser = new PezDispenser("Yoda");

that it's still a "Yoda" Pez Dispenser, and you can still change that.

Would it not be just easier in this case to do

 String characterName = "Yoda";
 System.out.printf("%s Dispenser", characterName);

because either way you're only changing "Yoda" to change the Pez Head.

What other, more practical, instances would you use Class sets?

2 Answers

Jon Sultana
Jon Sultana
6,916 Points

Their example is only to give you an idea of how a class can be implemented. An example that I have used before but with C++ was a MusicPlayer class. Within this class contained all the functionality of the music player like playAudio(), pauseAudio(), stopAudio(), etc. the idea of a class is to make code more legible and easier to use. The example that you gave above uses a String class. That String class makes manipulating or handling text("strings") more easier. That class has many member functions and variables that allow us to be able to complete some comparisons and many other actions that are needed. An example would be to search a string for a certain word. The String class contains the function contains("desired word") that completes this for us. without the class String you would have to write the code yourself in order to get the job done. With classes you can create your own Abstract Data Types(ADT). Hope this helps you getting a better understanding of why we may use a class.

Try to imagine if this project scaled larger. Imagine if you hand to create hundreds of Pez Dispensers. Would you rather try to come up with creative variable names for all the Pez Dispensers 100 times or just pass each name in a parameter 100 times.

More practically, would you rather have 100 McDonalds make 1 hamburger or just have 1 McDonalds make 100 hamburgers? Just like it would cost you significantly more money to create those 100 McDonalds only to complete the simple task of making 1 hamburger, it costs more computer memory to create more variables that essentially serve the same purpose. Just try to conceptualize that.