I knew the basics going in but the edge cases tripped me up.
Start by clearly distinguishing the three concepts: final is a keyword for immutability, finally is a block for cleanup, and finalize is a method for garbage collection. Then, for each, explain its purpose, behavior, edge cases, and best practices, emphasizing that finalize is deprecated and should be avoided. Conclude with a summary table or comparison to reinforce the differences.
Pro tip: Mention that finalize is deprecated since Java 9 and explain why (unpredictable, performance overhead), showing you keep up with modern Java practices. Also, highlight that finally always executes except in cases of JVM crash or System.exit(), demonstrating attention to edge cases.
Explain that final is a keyword used to declare constants, prevent inheritance, and prevent method overriding. Mention its behavior with variables, methods, and classes, and note that final variables must be initialized.
Describe finally as a block used in exception handling to ensure code execution regardless of whether an exception is thrown. Discuss its typical use for cleanup (e.g., closing resources) and edge cases like System.exit().
Explain finalize as a method called by the garbage collector before reclaiming an object's memory. Note that it's deprecated, unpredictable, and should not be relied upon for resource cleanup.
Summarize the differences in a structured way: final is about immutability and design, finally is about control flow and cleanup, finalize is about object lifecycle and is obsolete.
Recommend using final for constants and design, finally for resource management (or try-with-resources), and avoiding finalize in favor of explicit cleanup methods like close().
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.