Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Java know when a void method has finished its method body?

Tags:

java

methods

void

Let's say I have some code:

public int doSomething(int x)
{
    otherMethod(x);
    System.out.println("otherMethod is complete.");
    return 0;
}

public void otherMethod(int y)
{
    //method body
}

Since otherMethod's return type is void, how does the doSomething method know when otherMethod has completed, so it can go to the next like and print "otherMethod is complete."?

EDIT: Added return 0; to doSomething method so the example code will compile.

like image 475
reesjones Avatar asked Mar 22 '13 01:03

reesjones


People also ask

What is the return type of void method in Java?

The void keyword specifies that a method should not have a return value. Tip: If you want a method to return a value, you can use a primitive data type (such as int, char, etc.) instead of void, and use the return keyword inside the method: Read more about methods in our Java Methods Tutorial.

What does void mean in Java 1 1?

1. What does void mean in Java? In Java, void keyword is used with the method declaration to specify that this particular method is not going to return any value after completing its execution. We cant assign the return type of a void method to any variable because void is not a data type.

How do you return a value from a method in Java?

Tip: If you want a method to return a value, you can use a primitive data type (such as int, char, etc.) instead of void, and use the return keyword inside the method: Read more about methods in our Java Methods Tutorial.

How to mock a void method with Mockito?

But when we have to mock a void method, we can’t use when (). Mockito provides following methods that can be used to mock void methods. doAnswer (): We can use this to perform some operations when a mocked object method is called that is returning void. doThrow (): We can use doThrow () when we want to stub a void method that throws exception.


1 Answers

The parser knows where the end of execution is and even adds a return, for example:

 public static void main(String args[])  {

}

compiles to:

 public static main([Ljava/lang/String;)V
   L0
    LINENUMBER 34 L0
    RETURN <------ SEE?
   L1
    LOCALVARIABLE args [Ljava/lang/String; L0 L1 0
    MAXSTACK = 0
    MAXLOCALS = 1
}

And the same applies to your code (though I've added in the return 0 since your code doesn't compile):

 public int doSomething(int x)
    {
        otherMethod(x);
        System.out.println("otherMethod is complete.");
        return 0;
    }

    public void otherMethod(int y)
    {
        //method body
    }

compiled code:

public doSomething(I)I
   L0
    LINENUMBER 38 L0
    ALOAD 0
    ILOAD 1
    INVOKEVIRTUAL TestRunner.otherMethod (I)V
   L1
    LINENUMBER 39 L1
    GETSTATIC java/lang/System.out : Ljava/io/PrintStream;
    LDC "otherMethod is complete."
    INVOKEVIRTUAL java/io/PrintStream.println (Ljava/lang/String;)V
   L2
    LINENUMBER 40 L2
    ICONST_0
    IRETURN
   L3
    LOCALVARIABLE this LTestRunner; L0 L3 0
    LOCALVARIABLE x I L0 L3 1
    MAXSTACK = 2
    MAXLOCALS = 2

  // access flags 0x1
  public otherMethod(I)V
   L0
    LINENUMBER 46 L0
    RETURN <-- return inserted!
   L1
    LOCALVARIABLE this LTestRunner; L0 L1 0
    LOCALVARIABLE y I L0 L1 1
    MAXSTACK = 0
    MAXLOCALS = 2
}
like image 67
LuxuryMode Avatar answered Sep 19 '22 08:09

LuxuryMode