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

Chaz Hall
Chaz Hall
1,970 Points

Order of Methods

1) if(dispenser.isEmpty(){ System.out.println("It is currently empty");}

2) if (!dispenser.isEmpty()){ System.out.println("It is loaded");}

The 2) !dispenser.isEmpty. Does the program check the 1 to see if it is EMPTY first or does the program check 2) OR does it run the methods simultaneously. I want to know if there's a order of operations. Does it matter which method is listed in the class in the code? For example, 1 runs first because 2) is underneath 1) in the Workspace (or equivalent java code editor).

2 Answers

Well FIRST he checked 1) if(dispenser.isEmpty(){ System.out.println("It is currently empty");}

But then he used dispenser.load();

so dispenser changed (probably). Thats the reason why he made another if statement...this time he wanted to check if the dispenser is not empty (is loaded).

the dispenser in the first if statement has an other value than the one in the second statement.

But normal code will get executed from top to bottom... methods and loops are a different story

So if I'm understanding you Tobias, then the dispenser.isEmpty and !dispenser.isEmpty's run sequence is still in question. I also question how the program interprets when/how to run the line of code: System.out.println("It is currently empty"); in the code:

public static void main (String[]args){
System.out.println("We are making a Pez Dispenser.");
PezDispenser dispenser = new dispenser ("Yoda");
System.out.printf("The dispenser character is %s\n",
dispenser.getCharacterName());
if (dispenser.isEmpty()) {
System.out.println("It is currently empty"); }
System.out.println("Loading...");
dispenser.load();

Hi abbymann, the return value of dispenser is a String. isEmpty() is a method of the class String and returns a boolean (true/false).

It returns true if the length of the string is 0 .(https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#isEmpty--). So if dispenser (which just gets created two lines earlier) has no value (a "" String) So the if statement is true and it prints "It is currently empty"

Paul Ryan
Paul Ryan
4,584 Points

It will first do the first method and then do the second method. It would probably be cleaner to write it as:

if(dispenser.isEmpty(){ System.out.println("It is currently empty");}

else{ System.out.println("It is loaded");}

As if it is not empty, then it must have something in it.

Thats wrong (well not WRONG but in realationt to the video it is)because there is dispenser.load() after the first if statement which changes everything... The dispenser was empty first...but after dispenser.load() it had a value Craig wanted to check if the dispenser was empty before he made the .load() method and then check if it is full after the load() method

Paul Ryan
Paul Ryan
4,584 Points

Well I am just working off the code I can actually see

Yeah Paul, I was thinking that the program was missing a way to start the loading Pez sequence. Thanks for clarifying the if/else statement.