What will be printed and what will this method return?

What will be printed and what will this method return?


2-Line Answer

The method will print "Finally" and return 10. Even though the try block executes a return statement, the finally block always executes before the method actually returns.


Detailed Answer

This is one of the most frequently asked Java interview questions.

Almost everyone knows that the finally block executes. But the real interview starts when the interviewer asks, "If the return statement has already executed, how does finally still run?"

Let's understand what's actually happening inside the JVM.

At first glance, it looks like the method should return immediately.

return 10;

After all, return means the method is finished, right?

Not exactly.

When Java reaches the return statement, it doesn't immediately exit the method.

Instead, it performs three internal steps.

Step 1: Evaluate the Return Value

Java first evaluates the expression after return.

return 10;

The value 10 is calculated.

Nothing has been returned yet.

Java simply stores this value in a temporary location.

Think of it as keeping the return value aside for a moment.


Step 2: Execute the finally Block

Before the method exits, Java checks whether a finally block exists.

Since it does, execution moves there.


Output:

Finally

Only after the entire finally block finishes does Java continue with the pending return.


Step 3: Return the Stored Value

The value that was saved earlier is finally returned.

So the caller receives:

10

The important thing to remember is this.

The return statement doesn't immediately leave the method.

It first prepares the return value.

Then executes finally.

Then actually returns.


Internal Flow

Think of it like this:


This explains why both things happen.

The message is printed.

The value is still returned.


Why Does Java Work This Way?

Imagine you borrow a book from a library.

You decide to leave the library.

But before you're allowed to walk out, the librarian checks whether you've returned your library card.

That final check happens every single time.

Whether you're leaving normally...

Returning early...

Or even because of an exception.

The finally block works exactly like that final checkpoint.

It gets one last chance to execute before the method completely exits.

That's why developers commonly use it to close database connections, release files, or free other resources.


What If finally Also Has a Return Statement?

Now the interview gets interesting.

Suppose we change the code.

Now what happens?

The method returns: 20

Not 10.

Why?

Because the finally block executes after the try block and its return statement overrides the previously stored return value.

This is one of the biggest reasons why returning from a finally block is considered a bad practice.

It hides the original return value and makes the code confusing.


What If finally Throws an Exception?

Consider this example.

Will the method return 10?

No.

The exception thrown inside finally completely replaces the pending return.

The caller never receives 10.

Instead, the program receives the exception.


Common Interview Follow-up Questions

Q1. Does finally always execute?

Almost always.

It won't execute if the JVM is forcibly terminated using methods like System.exit() or if the JVM crashes.


Q2. Where is finally commonly used?

To clean up resources such as:

  • Database connections
  • File streams
  • Network sockets
  • Locks

Q3. Is returning from finally a good practice?

No.

It overrides the original return value and can even hide exceptions.

That's why it's strongly discouraged.


Q4. Which value is returned if both try and finally contain a return statement?

The value returned by the finally block.


Q5. What if try throws an exception but finally returns a value?

The exception is completely suppressed, and the caller receives the value returned from finally.

This is another reason why returning from finally is dangerous.


Real-World Example

Imagine you're leaving your office after work.

You've already packed your laptop and are ready to walk out.

But before you leave, security asks you to scan your ID card at the exit gate.

Only after that scan are you allowed to leave the building.

The return statement is like packing your laptop.

The finally block is the security checkpoint.

No matter how quickly you're trying to leave, you still have to pass through that final checkpoint before exiting.


Code Example

Output

Inside Try
Inside Finally
Returned Value: 10

Summary

A return statement inside a try block doesn't immediately end the method. Java first saves the return value, executes the finally block, and only then returns the stored value. The only exception is when the finally block itself returns a value or throws an exception, because in that case it overrides the original flow completely.




🚀 Crack Your Next Java Interview

Reading articles is great, but consistency is what gets you hired. I drop one real, company-tested Java interview question every single day on Instagram.

No fluff—just the exact questions tech panels are asking right now, broken down simply.

👉 Follow @JavaInterviewDaily on Instagram to build your daily prep habit.

Comments

Popular posts from this blog

deployment strategy for a Spring Boot application

Why is String Immutable in Java?

Mastering the Basics: What is a Class and an Object in Java?