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 Harnessing the Power of Objects Exceptions

Bob Allan
Bob Allan
10,900 Points

Why do Example.java and PezDispenser.java run at the same time?

In the Pez dispenser exercises, I don't understand why both files run when only Example.java is compiled/ran. Are they linked somehow?

How would I run both of them in an IDE like jGrasp or Eclipse?

In JavaScript it is easy to see how the HTML/CSS/JS are all tied together. I assume that Java works completely differently, but I don't know how.

Thanks in advance for your help.

1 Answer

William Bauer
William Bauer
1,178 Points

So, in Java when compiling - your system will look for the method (public static void Main()). So this means that the class with this method in it is the main class for your program. The PezDispenser is connected with Example by this code: PezDispenser dispenser = new PezDispenser();

Here you create a NEW dispenser which is coming from the class PezDispenser. I hope this made sense. Enjoy the rest of your day!