Kyle Hoover Kyle Hoover - 3 months ago 6x
Java Question

Will this.method2(); skip the return value?

In this example of code. What will be read right after "this.method2();"? Will it jump to method2() before returning the returnedValue?

public int method1(int returnedValue) {
// Do something
returnedValue = 1;
this.method2(); // Where will it go after this line?
return returnedValue;

public void method2() {
// Do something

public static void main(String[] args) {
Stuff sp = new Stuff();
System.out.print(returned value);


Now your main method doesn't call method1, consider instead this main method :

public static void main(String[] args) { new Main().method1}

Assuming Main is the main class, what will happen on this line :


Is that method2 gets called before method1 returns a value because you didn't call return before calling method2.

If method1 was defined like this :

public void method1(int returnedvalue)
    return returnedvalue;

Then method1 will return returnedvalue without calling method2(the above code is bad ofcourse as this.method2() is unreachable).

After method2 finishes and returns, method1 execute the next statement which is return returnedvalue, it will return and execution continues in main.

As others said ofcourse, if exception was raised in method2 without being caught in it, The exception will propagate from method2 to method1, if method1 doesn't catch it it will propagate to main, if main doesn't catch it your app will crash :).