Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I check if a method returns true or false in an IF statement in Java?

Let's say I have a boolean method that uses an if statement to check whether the return type should be true or false:

public boolean isValid() {
   boolean check;
   int number = 5;

   if (number > 4){
      check = true;
   } else {
      check = false;
   }

 return check;

And now, I want to use this method as a parameter of an if statement in a different method:

if(isValid == true)  // <-- this is where I'm not sure
   //stop and go back to the beginning of the program
else
   //continue on with the program

So basically what I'm asking is, how do I check what the return type of the boolean method within the parameters of an if statement is? Your answers are deeply appreciated.

like image 446
Graham S. Avatar asked Nov 27 '12 06:11

Graham S.


1 Answers

Since it's a method, to call it you should use parens afterwards, so your code would then become:

if(isValid()) {
    // something
} else {
    //something else
}
like image 132
Owen Avatar answered Jan 09 '23 16:01

Owen