What is the difference between final, finally and finalize in Java?

NameKindMeaning
finalKeywordA constant variable, a method which can not be overridden, a class which can not be extended
finallyBlockRuns after try and catch, with or without an exception. For cleaning
finalize()MethodCalled by the GC before it removes an object. Deprecated for removal, do not use it

Use try with resources or a Cleaner instead of finalize().

public class Main {
    static int test() {
        try {
            return 1;
        } finally {
            System.out.println("finally runs, even after return");
        }
    }

    public static void main(String[] args) {
        final int limit = 10;
        System.out.println(limit + " " + test());
    }
}
Output

finally runs, even after return
10 1

See Java Exceptions.


Learn more about the similar topics:
Tutorials
No Content Found.
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.