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

Jian Chen
Jian Chen
4,938 Points

Why make a method when you can just make variable?

so we made a method to check if the Pez Dispenser is empty or not with: public boolean isEmpty() { return mPezCount == 0; } But why don't we just make a variable like this? boolean isEmpty = mPezCount == 0; both ways we still know if it is empty or not, right? Is there something I'm missing or misunderstanding? I would appreciate any clarification.

1 Answer

Rob Bridges
.a{fill-rule:evenodd;}techdegree seal-36
Rob Bridges
Full Stack JavaScript Techdegree Graduate 35,467 Points

Hey Jian,

we certainly could have done that that way there are multiple ways to accomplish a task in any language, but this way we actually made a method that could be called on a PezDispenser object for us to check, it becomes difficult when we try to determine information about objects without passing methods on them.

Also another thing to consider, it sounds like the variable you wanted to make on the PezDispenser would have been a member variable, by default it's best practice to make these private,, so we would have still had to call a public method so that other classes in our code could access it.

Basically it comes down a method being needed in any case, either so that we can pass the method onto a PezDispenser object, or, so that other classes would have access to that portion of our code.

Thanks, let me know if this helps or not.