โ† Java
#6 most askedVery commonEasyCore Java & OOP

final vs finally vs finalize

New to Core Java & OOP? Learn it from scratch first
Inheritance vs Composition โ†’
๐Ÿข Asked in real interviews
No reports yet. Were you asked this? Be the first to say where.
I was asked this

Explain final, finally and finalize(). They sound alike but are unrelated.

๐Ÿ”ฎ Predict the output

public class Main {
    static int f() {
        try {
            return 1;
        } finally {
            System.out.println("cleanup");
        }
    }
    public static void main(String[] args) {
        System.out.println(f());
    }
}

๐ŸŽค Answer it out loud, then check yourself

Say your answer as if the interviewer is listening (30โ€“90 seconds). Then reveal what a strong answer covers and tick what you said.

    Answer out loud first, then compare.
    Want to rehearse it?
    Answer this to the AI interviewer and get a verdict โ†’