Michael N
Michael N

Reputation: 23

How to check a boolean method return value in java

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!

Upvotes: 2

Views: 15137

Answers (5)

mhyst
mhyst

Reputation: 297

Conditional structures like if, while, do..., etc receive a boolean value, so it isn't necessary to put "boolVar == true". Just doing "if (boolVar)" is enough. As for your example in the doSomething method, just doing "return boolVar;" would do the work, without the need of any ifs, unless you pretend to do some more things on it.

To check a function return value works in the same way. I mean, variables have a value and functions also, the only difference is that variables hold a value while functions calculate or generate a value. So, in your code:

public class testDoThings {
    public void check() {

       doThings someObject = new doThings();

        boolean flag = sameObject.doSomething();
        if (flag) {

            // print a statment

         } else {
           //If you want to check falsehood, !flag would do.
                             // Notice the "!" sign before 
                             // the flag variable?
                             // It is a NOT sign, so
                             // NOT true = false
                             // NOT false = true
                             // The if will execute its code 
                             // When the result value is true
                             // So when the function return val is                           
                             // false.

            // print a different statement           
        }
    }
}

I hope this explanation is enough clear.

Upvotes: -1

Deepanjan
Deepanjan

Reputation: 649

You can try something like this:

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

else{
    System.out.print("foo2");
}

Upvotes: 1

Mahdi
Mahdi

Reputation: 2295

Generally you compare two things using == operator: if (x == y) ... so we have:

if ( someObject.doSomething() == true ) {
    //statements
} else {
    //statement for the case where result of method call is false
}

BTW instead of if(x == true) you can simply write if(x).

Upvotes: 0

magicandflew
magicandflew

Reputation: 91

Here's one way:

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

else{
    System.out.print("foo2");
}

Upvotes: 0

Rahul Tripathi
Rahul Tripathi

Reputation: 172528

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           
}

Upvotes: 3

Related Questions