How to check a boolean method return value in java

13,315

Solution 1

You can try like this:

if (someObject.doSomething()) //So if your function returns true then the below statement will be executed
{ 
   // print a statment
}
else //This will check for the false condition of your function
{
   // print a different statement           
}

Solution 2

You can try something like this:

if(someObject.doSomething()){
    System.out.print("foo1");
}

else{
    System.out.print("foo2");
}
Share:
13,315

Related videos on Youtube

Michael N
Author by

Michael N

Updated on May 25, 2022

Comments

  • Michael N
    Michael N almost 2 years

    I am pretty new to Java what I am trying to do may seem really strange but it is a matter of me understanding a little bit about how Java works more than actually accomplishing a set result.

    If I have a boolean method for instance:

    public class doThings
    {
    
        // imagine the intial value of this variable is set
        // and or modified using getters and setters between the declaration
        // and the following method
        private boolean boolVar;
    
    
        public boolean doSomething()
        {
            if(boolVar == true)
            {
                //does things and then returns true
                return true;
            }
            else
            {
                return false;
            }
        }
    }
    

    And I want to call this method in another class like so...

    public class testDoThings
    {
    
        doThings someObject = new doThings;
        public static void main(String[] args)
        { 
            someObject.doSomething()
        }
    } 
    

    How do I check (in the class testDoThings) to see if the method has returned true or returned false and print messages accordingly something like this...

    public class testDoThings
    {
    
        doThings someObject = new doThings;
        public static void main(String[] args)
        {
    
            someObject.doSomething()
            if (doSomething() returns true) // yes I am aware this is not
                                            //valid
            { 
                // print a statment
            }
            else if (doSomething() returns false) // yes once again not valid
            {
                // print a different statement           
            }
        }
    } 
    

    I am aware that you could put these messages in the class containing the method but if I want different messages depending on where the method is called and what it is a called on then putting things in the original class method is not always going to work.

    If I am completely off the track here please let me know, if someone can explain this to me though, that would be great!

  • Daniel Z.
    Daniel Z. almost 8 years
    Which would be the same as if(someObject.doSomething() == true){ }else{ }. For more operators check out this site: link
  • Aakash
    Aakash almost 8 years
    Why do you need an "else..if"? Since the condition (method call) is boolean, a simple else would suffice. Also, you don't, probably, want to call the same method twice, just to check the return.
  • Scary Wombat
    Scary Wombat almost 8 years
    This is potentially bad as it may cause doSomething to actually be executed twice.
  • mhyst
    mhyst almost 8 years
    I agree. to both comments. I am professional coder. I just tried to fill the holes. All can be improved. Let me fix it.
  • mhyst
    mhyst almost 8 years
    I don't understand how can people downvote the more explanatory and the best understandable for newbies in here. But hey, that's OK. Downvote if you fell like it.
  • Daniel Z.
    Daniel Z. almost 8 years
    They propably downvoted it before you edited your post. But anyway, as for now, youre code still wouldnt compile, because you have to wrap it in a method. If you fix it and also your comments youll get an upvote.
  • mhyst
    mhyst almost 8 years
    Ok, I did. I hope people don't get mad at me.