<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: rahul khattri</title>
    <description>The latest articles on DEV Community by rahul khattri (@rahulkhattri0).</description>
    <link>https://dev.to/rahulkhattri0</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F727040%2F308e5d14-7064-45a4-b71a-fbd1a287c2ca.jpeg</url>
      <title>DEV Community: rahul khattri</title>
      <link>https://dev.to/rahulkhattri0</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rahulkhattri0"/>
    <language>en</language>
    <item>
      <title>🚫 When Should You Use finalize() in Java?</title>
      <dc:creator>rahul khattri</dc:creator>
      <pubDate>Wed, 03 Sep 2025 18:06:28 +0000</pubDate>
      <link>https://dev.to/rahulkhattri0/when-should-you-use-finalize-in-java-369p</link>
      <guid>https://dev.to/rahulkhattri0/when-should-you-use-finalize-in-java-369p</guid>
      <description>&lt;p&gt;&lt;strong&gt;TL;DR: Never!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The finalize() method in Java was originally designed to let developers clean up resources before an object is reclaimed by the Garbage Collector.&lt;/p&gt;

&lt;p&gt;But here’s the catch:&lt;/p&gt;

&lt;p&gt;⚠️ It is not guaranteed to be called.&lt;/p&gt;

&lt;p&gt;⚠️ Its execution is totally unpredictable.&lt;/p&gt;

&lt;p&gt;⚠️ Relying on it means your resources (like file handles, DB connections, sockets) may never actually close.&lt;/p&gt;

&lt;p&gt;Instead of finalize(), the modern and reliable approach is:&lt;/p&gt;

&lt;p&gt;✅ Try-with-resources (introduced in Java 7):&lt;/p&gt;

&lt;p&gt;&lt;code&gt;try (FileInputStream fis = new FileInputStream("data.txt")) {&lt;br&gt;
    // use the resource&lt;br&gt;
} catch (IOException e) {&lt;br&gt;
    e.printStackTrace();&lt;br&gt;
}&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This ensures that resources are automatically closed once the block is done — no surprises, no waiting for the GC.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
