monica
monica

Reputation: 502

Return statement in void method

I have the following method that returns void and I need to use it in another method that also returns void. Can I do the following?

public void doSomething(){}

public void myMethod()
{
    return doSomething();
}

Thanks for all your comments, but let me be more specific

I only doSomething if something happens, otherwise I do other things

public void doSomething(){}

public void myMethod()
{
    for(...)
        if(somethingHappens)
        {
            doSomething();
            return;
        }

    doOtherStuff();
}

Instead of the code above, can I just write return doSomething(); inside the if statement?

Upvotes: 3

Views: 35901

Answers (5)

Krzysztof Jabłoński
Krzysztof Jabłoński

Reputation: 1941

If

  • the condition (somethingHappens) actually depends on what doSomething() did
  • and doSomething() is a method you can modify

then you should consider making it return the boolean (or any suitable type) value which tells the callee method if it happened or not.

Code would then look somewhat like this:

public boolean doSomething() {
    // internal working
    if (internalCondition) {
         // something done here
         return true;
    }
    // not done ;
    return false;
}

public void myMethod()
{
    for(...)
        if (doSomething())
        {
            return;
        }
    // didn't hit 'doSomething() == true'
    doOtherStuff();
}

Upvotes: 0

MattS
MattS

Reputation: 717

No, returns are unnecessary in Java void functione. Instead, do this:

public void myMethod(){
    doSomething();
}

You could put a return after doSomething() if you wanted, but as I said it'd be unnecessary.

Upvotes: 2

Samy Vilar
Samy Vilar

Reputation: 11130

void functions don't/can't return anything as such. Simply call the function.

Upvotes: 5

kol
kol

Reputation: 28688

No, just do this:

public void doSomething() { }

public void myMethod()
{
    doSomething();
}

or in the second case:

public void doSomething() { }

public void myMethod()
{
    // ...
    if (somethingHappens)
    {
        doSomething();
        return;
    }
    // ...
}

"Returning void" means returning nothing. If you would like to "jump" out of myMethod's body, use return; The compiler does not allow writing return void; ("illegal start of expression") or return doSomething(); ("cannot return a value from method whose result type is void"). I understand it seems logical to return "void" or the "void result" of a method call, but such a code would be misleading. I mean most programmers who read something like return doSomething(); would think there is something to return.

Upvotes: 15

Matt Westlake
Matt Westlake

Reputation: 3651

You either have to just use doSoemthing like the others have pointed out OR

public (insert data type like int, double, string, etc) myMethod()
{
     return doSomething();
}

If do something returns an int

// if you wanted to take 2 numbers and add them

public void myMethod()
{
    valueOfX+Y = doSomething();
}

public int doSomething()
{
    x = 2;
    y = 2;
    return X+Y;
}

hope that clears it up

Upvotes: 0

Related Questions