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) Harnessing the Power of Objects Helper Methods and Conditionals

I am confused by why we are working with two files (Example and PezDispenser). Can anyone explain what each is doing?

I keep reviewing the videos and I believe that one (PezDispenser) is a Class; however, uncertain how we are calling it in the Example.

Mauro Teixeira
Mauro Teixeira
3,727 Points

I didn't follow this video but I suppose I can help the same.

You are right in saying that PezDispenser is a class. Basically, this class offers everything that you need (and if it doesn't, you can modify it) to work with a "PezDispenser". This is why Object Oriented programming languages are so attractive, because you can organize your code in a way that every property or method you need to work with "...something..." can be put in a Class... Say you want to work with a "House", you define a class "House" and write all the properties (for example, how many rooms, how many showers) and methods (for example openFrontDoor(), openGarageDoor(), turnOffAllLights(), etc). If you want to work with a "Car", you can also define a class Car.

However, classes are not by itself executable: you can't "run" a class in your console... (or more correctly, you can if you define a public static void Main method).

Now, Example,java is basically just a file that declares a public static void Main method, which is by default the method that Java will execute. And if you notice, in the video the teacher puts executable code in this method that calls the PezDispenser class and it's methods.

If you do PezDispenser dispenser = new PezDispenser(), you essentually just created an Object of type/class PezDispenser, and you can now access the classe's public properties and methods to interact with the PezDispenser.

Thanks so much Mauro.

Your statement: "However, classes are not by itself executable: you can't "run" a class in your console... (or more correctly, you can if you define a public static void Main method)." is particularly helpful.

I couldn't wrap my head around why he ran PezDispenser in the repl but not in the console.

So, once a Class is created, it can only be called from the main file for execution?

Mauro Teixeira
Mauro Teixeira
3,727 Points

Well, no, not necessarily, you can call a class from another class.

For example, you can have a class Car and a class Person, and inside the Car you can have a driver, which is a "Person", and a list of passenger which are also "Person". So classes can be "instanciated"/called by other classes:

public class Car {

private int numberOfSeats;
private Person driver = new Person("Laura", "Bullock");
private List<Person> passengers = new ArrayList<Person>();

// Person here is a class that can also be defined the same way I'm here defining Car
}

However, in regular console java programs there is always going to be a method that is the "start" of it all, and it is regularly this public static void Main method that the teacher declared in the Example.java.