{"id":187,"date":"2025-06-22T13:18:12","date_gmt":"2025-06-22T13:18:12","guid":{"rendered":"https:\/\/thetestdata.com\/blog\/?p=187"},"modified":"2025-07-16T10:30:37","modified_gmt":"2025-07-16T10:30:37","slug":"what-is-final-in-java","status":"publish","type":"post","link":"https:\/\/thetestdata.com\/blog\/what-is-final-in-java\/","title":{"rendered":"What is final in java?"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Let\u2019s explain <strong><code>final<\/code> in Java<\/strong> in <strong>easy, everyday language<\/strong> \u2014 with clear examples so you fully get it.<\/h2>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u2705 What Does <code>final<\/code> Mean in Java?<\/h2>\n\n\n\n<h4 class=\"wp-block-heading\">In simple terms:<\/h4>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<h4 class=\"wp-block-heading\"><strong><code>final<\/code> means \u201cyou can\u2019t change it later.\u201d<\/strong><\/h4>\n<\/blockquote>\n\n\n\n<h4 class=\"wp-block-heading\">It is used when you want to make something <strong>unchangeable<\/strong> \u2014 like a <strong>lock<\/strong> that prevents future changes.<\/h4>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udee0\ufe0f Where Can You Use <code>final<\/code> in Java?<\/h2>\n\n\n\n<h4 class=\"wp-block-heading\">You can use <code>final<\/code> with:<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Variables<\/strong> (can\u2019t change the value)<\/li>\n\n\n\n<li><strong>Methods<\/strong> (can\u2019t override them)<\/li>\n\n\n\n<li><strong>Classes<\/strong> (can\u2019t extend\/inherit them)<\/li>\n<\/ol>\n\n\n\n<p>Let\u2019s break them down one by one.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd39 1. <code>final<\/code> Variable<\/h2>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Once you assign a value to a <code>final<\/code> variable, you can\u2019t change it.<\/strong><\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udce6 Example:<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>public class Demo {<br>    public static void main(String[] args) {<br>        final int speedLimit = 60;<br>        \/\/ speedLimit = 80; \/\/ \u274c Error: can't change a final variable<br>        System.out.println(\"Speed Limit is: \" + speedLimit);<br>    }<br>}<br><\/code><\/pre>\n\n\n\n<p>\u2714\ufe0f <code>speedLimit<\/code> is final \u2014 you can set it <strong>once<\/strong>, but not change it.<\/p>\n\n\n\n<p>This is useful when you want to declare <strong>constants<\/strong> like PI, tax rate, or max limit.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd39 2. <code>final<\/code> Method<\/h2>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>You can mark a method as <code>final<\/code> so that no subclass can override it.<\/strong><\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udce6 Example:<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>class Vehicle {<br>    final void start() {<br>        System.out.println(\"Vehicle started\");<br>    }<br>}<br><br>class Car extends Vehicle {<br>    \/\/ void start() { } \/\/ \u274c Error: can't override final method<br>}<br><\/code><\/pre>\n\n\n\n<p>\u2714\ufe0f Once a method is marked <code>final<\/code>, no class that inherits it can change its behavior.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd39 3. <code>final<\/code> Class<\/h2>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>A <code>final<\/code> class cannot be extended (inherited).<\/strong><\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udce6 Example:<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>final class Animal {<br>    void sound() {<br>        System.out.println(\"Animal sound\");<br>    }<br>}<br><br>\/\/ class Dog extends Animal {} \/\/ \u274c Error: can't extend a final class<br><\/code><\/pre>\n\n\n\n<p>\u2714\ufe0f You use a <code>final<\/code> class when you don\u2019t want anyone to change or extend your class \u2014 it&#8217;s <strong>locked<\/strong>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd04 Bonus: <code>final<\/code> with Objects<\/h2>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<h4 class=\"wp-block-heading\">If you make an object <code>final<\/code>, you can\u2019t change the object reference, but you can change the data inside.<\/h4>\n<\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udce6 Example:<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>public class Demo {<br>    public static void main(String[] args) {<br>        final StringBuilder sb = new StringBuilder(\"Hello\");<br>        sb.append(\" World\"); \/\/ \u2705 Allowed<br>        \/\/ sb = new StringBuilder(\"Hi\"); \/\/ \u274c Not allowed<br>        System.out.println(sb); \/\/ Output: Hello World<br>    }<br>}<br><\/code><\/pre>\n\n\n\n<p><strong>\u2714\ufe0f The object is final \u2014 you can\u2019t reassign it \u2014 but you can change what&#8217;s inside it.<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83e\udde0 Summary<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Use of <code>final<\/code><\/th><th>What it does<\/th><th class=\"has-text-align-center\" data-align=\"center\">Example<\/th><\/tr><\/thead><tbody><tr><td>Variable<\/td><td>Can&#8217;t change its value<\/td><td class=\"has-text-align-center\" data-align=\"center\"><code>final int x = 10;<\/code><\/td><\/tr><tr><td>Method<\/td><td>Can&#8217;t override it in subclass<\/td><td class=\"has-text-align-center\" data-align=\"center\"><code>final void run()<\/code><\/td><\/tr><tr><td>Class<\/td><td>Can&#8217;t be extended (inherited)<\/td><td class=\"has-text-align-center\" data-align=\"center\"><code>final class Animal<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83c\udfaf Think of <code>final<\/code> as:<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A <strong>lock<\/strong> \ud83d\udd12 \u2014 once used, <strong>no one can change or override<\/strong> it.<\/li>\n\n\n\n<li>A way to <strong>protect values, methods, or class designs<\/strong>.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Learn what the final keyword means in Java. Discover how it restricts changes to variables, methods, and classes to ensure immutability and design integrity.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"site-container-style":"default","site-container-layout":"default","site-sidebar-layout":"default","disable-article-header":"default","disable-site-header":"default","disable-site-footer":"default","disable-content-area-spacing":"default","footnotes":""},"categories":[17],"tags":[],"class_list":["post-187","post","type-post","status-publish","format-standard","hentry","category-java-interview-questions"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/posts\/187","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/comments?post=187"}],"version-history":[{"count":7,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/posts\/187\/revisions"}],"predecessor-version":[{"id":460,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/posts\/187\/revisions\/460"}],"wp:attachment":[{"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/media?parent=187"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/categories?post=187"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/tags?post=187"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}