This is the fourth day of my participation in Gwen Challenge. This article is participating in “Java Theme Month – Java Development in Action”, see the activity link for details.

People are often asked in an interview: Finally or return, who executes first?

To solve this problem, we can first think about what finally is used for. It is used to end some normal closing action or closing sign. That is, finally will be executed last no matter what. For example, when operating a database, you need finally to release resources after connecting to the database using Jdbc connection pool. Another example is redis connection, after obtaining the increase, deletion, change and check of the data processed by the connection pool, it needs to release its connection pool.

But what if return comes before finally? Or after finally? Let’s look at return before finally, as in:

public class App { public static void main(String[] args) { System.out.println("return result: " + test()); } public static int test() { try { Thread.sleep(1); System.out.println(" return 1"); return 1; } catch (InterruptedException e) { e.printStackTrace(); return -1; } finally {system.out. println(" finally"); }}}Copy the code

In the example above, we can see the output:

Run the return 1 command. Finally return result: 1 commandCopy the code

That is, finally is executed before return.

We are looking at a return inside a finally if there is a return in front of it:

public static int test() { try { Thread.sleep(1); System.out.println(" return 1"); return 1; } catch (InterruptedException e) { e.printStackTrace(); return -1; } finally {system.out. println(" finally"); return 3; }}Copy the code

The result is:

Run the return 1 command. Finally return result: 3Copy the code

Return In a try{}, finally, a return may be executed first, and then finally.

Let’s see return after finally, e.g.

public static int test() { try { Thread.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } finally {system.out. println(" finally"); } system.out. println(" return 2"); return 1; }Copy the code

Output result:

Finally run the return 2 return result: 1 commandCopy the code

Conclusion: When finally comes after a return, the return is executed after finally; When finally contains a return, the end is immediately executed after the return. Finally before return, finally and then return.

Java final, finally, Finalize, finalize

  • Final is used to declare properties, methods, and classes, indicating that properties are immutable, methods are not overridden, and classes are not inherited, respectively. That is, if a class is declared final, it cannot be inherited as a parent, so a class cannot be declared abstract and final at the same time. Variables or methods are declared final to ensure that they are not modified in use. Variables that are declared final must be given an initial value at declaration time and can only be read in future references. Methods declared final can also be used only and cannot be overridden.

  • Finally is part of the exception handling statement structure, always executed, common scenario: release some resources, such as previously mentioned redis, DB, etc. A finally block is provided during exception handling to perform any cleanup, that is, a finally block is executed after a catch.

  • Finalize is a method of Object class. When garbage collector executes, Finalize will call the method of the Object to be collected, and can overwrite the method to provide other resource collection during garbage collection, such as closing files.