{"id":311,"date":"2025-06-29T14:38:59","date_gmt":"2025-06-29T14:38:59","guid":{"rendered":"https:\/\/thetestdata.com\/blog\/?p=311"},"modified":"2025-07-16T10:15:18","modified_gmt":"2025-07-16T10:15:18","slug":"what-is-the-difference-between-static-binding-and-dynamic-binding","status":"publish","type":"post","link":"https:\/\/thetestdata.com\/blog\/what-is-the-difference-between-static-binding-and-dynamic-binding\/","title":{"rendered":"What is the difference between static binding and dynamic binding?"},"content":{"rendered":"\n<p>In Java, <strong>binding<\/strong> refers to the process of connecting a method call to the method body. The key difference between <strong>static binding<\/strong> and <strong>dynamic binding<\/strong> lies in <em>when<\/em> this connection is made\u2014<strong>compile time<\/strong> vs <strong>runtime<\/strong>.<\/p>\n\n\n\n<p>\ud83d\udd39 <strong>Static Binding (Early Binding)<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>When?<\/strong> Happens at <strong>compile time<\/strong>.<\/li>\n\n\n\n<li><strong>What?<\/strong> The method call is resolved by the compiler based on the reference type.<\/li>\n\n\n\n<li><strong>Applies to:<\/strong>\n<ul class=\"wp-block-list\">\n<li><code>private<\/code>, <code>final<\/code>, and <code>static<\/code> methods<\/li>\n\n\n\n<li>Method <strong>overloading<\/strong><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Why?<\/strong> These methods cannot be overridden, so the compiler knows exactly which method to call.<\/li>\n<\/ul>\n\n\n\n<p>\u2705 Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Animal {\n    static void sound() {\n        System.out.println(\"Animal makes sound\");\n    }\n}\n\nclass Dog extends Animal {\n    static void sound() {\n        System.out.println(\"Dog barks\");\n    }\n}\n\npublic class Test {\n    public static void main(String&#91;] args) {\n        Animal a = new Dog();\n        a.sound(); \/\/ Output: Animal makes sound\n    }\n}<\/code><\/pre>\n\n\n\n<p>Even though <code>a<\/code> refers to a <code>Dog<\/code>, the method is <code>static<\/code>, so the compiler binds it to <code>Animal.sound()<\/code>.<\/p>\n\n\n\n<p>\ud83d\udd38 <strong>Dynamic Binding (Late Binding)<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>When?<\/strong> Happens at <strong>runtime<\/strong>.<\/li>\n\n\n\n<li><strong>What?<\/strong> The method call is resolved based on the actual object, not the reference type.<\/li>\n\n\n\n<li><strong>Applies to:<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>Overridden<\/strong> methods (non-static, non-final)<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Why?<\/strong> The JVM decides which method to call based on the object\u2019s actual class.<\/li>\n<\/ul>\n\n\n\n<p>\u2705 Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Animal {\n    void sound() {\n        System.out.println(\"Animal makes sound\");\n    }\n}\n\nclass Dog extends Animal {\n    @Override\n    void sound() {\n        System.out.println(\"Dog barks\");\n    }\n}\n\npublic class Test {\n    public static void main(String&#91;] args) {\n        Animal a = new Dog();\n        a.sound(); \/\/ Output: Dog barks\n    }\n}<\/code><\/pre>\n\n\n\n<p>Here, the method is resolved at runtime based on the actual object (<code>Dog<\/code>), not the reference type (<code>Animal<\/code>).<\/p>\n\n\n\n<p>\ud83e\udde0 Summary Table<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Feature<\/th><th>Static Binding<\/th><th>Dynamic Binding<\/th><\/tr><\/thead><tbody><tr><td><strong>Time of Binding<\/strong><\/td><td>Compile time<\/td><td>Runtime<\/td><\/tr><tr><td><strong>Applies To<\/strong><\/td><td>Static, final, private methods<\/td><td>Overridden methods<\/td><\/tr><tr><td><strong>Polymorphism Type<\/strong><\/td><td>Compile-time (Overloading)<\/td><td>Runtime (Overriding)<\/td><\/tr><tr><td><strong>Performance<\/strong><\/td><td>Slightly faster<\/td><td>Slightly slower (due to lookup)<\/td><\/tr><\/tbody><\/table><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Discover the difference between static and dynamic binding in Java. Learn how each impacts method resolution, compile-time vs runtime behavior, and flexibility<\/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-311","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\/311","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=311"}],"version-history":[{"count":2,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/posts\/311\/revisions"}],"predecessor-version":[{"id":432,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/posts\/311\/revisions\/432"}],"wp:attachment":[{"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/media?parent=311"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/categories?post=311"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thetestdata.com\/blog\/wp-json\/wp\/v2\/tags?post=311"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}