Java final, finally, and finalize differences

#Interview-137: Java - What's the difference between final, finally and finalize? The difference between final, finally, and finalize is mainly about their purpose — one is a keyword, one is a block, and one is a method. final (Keyword): restrict something from being changed. • Final variable → value cannot be changed (constant) • Final method → cannot be overridden • Final class → cannot be inherited finally (Block): used in exception handling, and it always executes whether an exception occurs or not. • Used with try-catch • Typically used for cleanup (closing files, DB connections) finalize() (Method): was used for garbage collection cleanup before an object is destroyed. Deprecated in modern Java (Java 9+) In modern Java, instead of finalize(), we prefer using try-with-resources or explicit cleanup methods because finalize() is unreliable and deprecated. #interviewprep #interview #testing #qajobs #jobs #jobsearch #jobseekers #hiring #hiringnow #lookingforjob #manualtesting #testautomation #bdd #cucumber #testng #etltesting #performance #apitesting #softwaretesting #manualtester #qatester

To view or add a comment, sign in

Explore content categories