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

Isam Al-Abbasi
Isam Al-Abbasi
2,058 Points

Why are we using 2 classes to make the dispenser?? i.e Example & PezDispenser classes!!

Hello everyone,

Apologies if my questions seems stupid but I am new to programming and I was wondering why are we using 2 separate classes to make pez dispenser? Steve Hunter

5 Answers

Hi Isam,

From memory, we're using the Example class to hold the main method that generates the instances of PezDispenser. If we're creating examples of creating instances/objects of classes, we need somewhere to do that. So, we use Example to do the running of the application, calling the right methods at the right time, as well as holding both the main method and the instance of PezDispenser.

Make sense?

Steve.

To add on to Steve's remarks:

First, as the instructor emphasized in the video, classes are supposed to be about one thing and one thing only. So, for example, if you were writing a program for geometric shapes, you would want separate classes for Rectangle, Circle, Triangle, etc.

Second, the PezDispenser class, as are all true classes, is a blueprint for pez dispenser objects. You use the class to create one or more objects, or instances of the class. Although you could do everything in one class, you shouldn't. You need a second class, like Example.

Third, although Treehouse doesn't make this distinction, PezDispenser is a true class, while Example is a program. The former can be compiled but not run. The latter can be both compiled and run. Example can be run, or executed, because it has a main() method. PezDispenser doesn't, and so it cannot be run.

So, putting this all together, you create pez dispenser objects in the Example program using the PezDispenser class to create them.

For the record there's a third kind of "class", viz., a module -- this is a class whose methods are static, and when you call those methods you don't have to create an object to call them on. You use the syntax ClassName.methodName() instead. An example would be Math.sqrt() or Math.round(). Here, unlike PezDispenser, you call the methods directly on the class, rather than creating a Math object and calling the method on that object.

Hope this helps.

That is a great explanation!

Isam Al-Abbasi
Isam Al-Abbasi
2,058 Points

Thank you Steve, it does make sense a little but I hope it will start making more sense as I dive into this language more. so basically every project should have 2 classes right!!

For the sake of an easy answer right now, let's go with "Yes". You'll find that you will end up with many, many more than that in reality once you get beyond command line stuff; but that just adds to the fun.

For now, you have a class that holds the run-order of the the application inside the main method and then classes separate to that which determine the capabilities of the objects.

Isam Al-Abbasi
Isam Al-Abbasi
2,058 Points

I get it now... well then let's keep watching Craig and see how many classes we're gonna end up with to get that pez dispenser ready :)

I don't remember with that example, but I'd struggle to think of a use for many more classes. The Pez Dispenser example is just a vehicle for demonstrating some functionality. There may be extra classes added; I don't remember.

Isam Al-Abbasi
Isam Al-Abbasi
2,058 Points

Thank you so much jcorum for your thorough explanation. It sure helps.