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 Incrementing and Decrementing

Aditya Puri
Aditya Puri
1,080 Points

An error in his code which ain't showing up..

SO I found this probable error in his code which is not showing up..

He used the method "isEmpty" in the method "dispense" before he declared it.

Shouldn't this give an error as java reads the code from the top to bottom? WHy isn't an error showing up?

3 Answers

Matt Sorrell
Matt Sorrell
6,501 Points

Not sure if you still need help clarifying this, but here goes. I don't think it matters the order the methods are created in because when you create a new instance of the object in the main method it will gain access to all of them.

Aditya Puri
Aditya Puri
1,080 Points

i got it. The answer is that java is a compiled language and the compiler reads the code more than once so it can connect all the methods with their calls. I was getting confused as I thought that java worked like an interpreted language and was not familiar with the idea of compiled languages

Zachary Martin
Zachary Martin
3,545 Points

I think I know what you're referring too, he created the method in the The PezDispenser class so he didn't have to declare it in the Example java file.

Aditya Puri
Aditya Puri
1,080 Points

noo im not talking about "Example java file.". I am talking about the PezDispenser class file.In the PezDispenser class file he used the isEmpty helper method in the method dispense before he declared it. Won't that give an error. Shouldn't you use something AFTER you have declared it?

ALBERT QERIMI
ALBERT QERIMI
49,872 Points

Java is a Object oriented programing language