{"id":77682,"date":"2020-04-13T10:00:37","date_gmt":"2020-04-13T04:30:37","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=77682"},"modified":"2020-04-13T10:00:37","modified_gmt":"2020-04-13T04:30:37","slug":"static-and-dynamic-binding-in-java-differences-and-examples","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/static-and-dynamic-binding-in-java-differences-and-examples\/","title":{"rendered":"Static and Dynamic Binding in Java &#8211; Differences and Examples"},"content":{"rendered":"<p>Binding means an association of method call to the method definition. The picture below clearly shows what is binding. There are two types of Binding: <strong>Static and Dynamic Binding in Java<\/strong>.<\/p>\n<p>If the compiler maps the method at compile-time, it is Static Binding or early binding. And, if the method is resolved at runtime, it is Dynamic Binding or late binding.<\/p>\n<p>We have discussed the topic of Polymorphism in Java. Polymorphism in Java is one of the OOPs features that allows an object to occur in multiple forms. When a method exhibits polymorphism, the compiler maps the name of the method to the final implementation.<\/p>\n<p>In this article, we will discuss how the static and dynamic binding in Java are different from each other. So let\u2019s start with a detailed introduction about both of them.<\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/04\/java-static-vs-dynamic-binding.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-78270\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/04\/java-static-vs-dynamic-binding.jpg\" alt=\"Static and dynamic binding in java\" width=\"802\" height=\"420\" \/><\/a><\/p>\n<h3>What is Static Binding or Early Binding in Java<\/h3>\n<p>Static Binding or Early Binding in Java refers to a process where the compiler determines the type of object and resolves the method during the compile-time. Generally, the compiler binds the overloaded methods using static binding.<\/p>\n<p>There is a fact that the binding of static, private, and final methods are always done during compile-time using static-binding.<\/p>\n<h3>Why is the binding of static, final and private methods always a static binding?<\/h3>\n<p>The reason for the binding of private, final and static methods during the compile-time is that the compiler determines the type of the class at the compile-time and therefore we can not override them during the runtime.<\/p>\n<p>Another reason is that the static binding of methods provides better performance than the runtime binding. The compiler becomes aware of these methods and understands that method overriding is not possible with such methods.<\/p>\n<p>These methods can only be accessed by the object of the local class. Therefore the binding of these methods always takes place during compilation.<\/p>\n<h3>Example of Static Binding in Java<\/h3>\n<p>Suppose we have two classes named Person and Teacher. The Teacher class extends the Person class. Both of these classes have the same methods called speak(). But, this method is static so we can not override it.<\/p>\n<p>Therefore, even if we use the object of the Teacher class then also it calls the method of the Person class.<\/p>\n<p><strong>Code to understand the static binding in Java<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">package com.techvidvan.binding;\nclass Person\n{\n  public void speak()\n  {\n    System.out.println(\"Person speaks\");\n  }\n}\nclass Teacher extends Person\n{\n  public static void speak()\n  {\n    System.out.println(\"Teacher speaks\");\n  }\n}\npublic class StaticBinding\n{\n  public static void main( String args[ ])\n  {\n    \/\/ Reference is of Person type and object is Teacher type\n    Person obj = new Teacher();\n    obj.speak();\n    \/\/Reference and object both are of Person type.\n    Person obj2 = new Person();\n    obj2.speak();\n  }\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">Person speaks<br \/>\nPerson speaks<\/div>\n<p><strong>From the above code, we got the same output from the parent class. This happened because:<\/strong><\/p>\n<ul>\n<li>The reference for the parent class and the child class is the same(Person). That is, a single object refers to both of them.<\/li>\n<li>Since the method is static, the compiler is aware that this method can not be overridden in the child class and it knows which method to call. Therefore there is no ambiguity and the output is the same for both cases.<\/li>\n<\/ul>\n<h3>Dynamic Binding or Late Binding in Java<\/h3>\n<p>When the compiler resolves the method call binding during the execution of the program, such a process is known as Dynamic or Late Binding in Java. We also call Dynamic binding as Late Binding because binding takes place during the actual execution of the program.<\/p>\n<p>The best example of Dynamic binding is the Method Overriding where both the Parent class and the derived classes have the same method. And, therefore the type of the object determines which method is going to be executed.<\/p>\n<p>The type of object is determined during the execution of the program, therefore it is called dynamic binding.<\/p>\n<h3>Example of Dynamic Binding in Java<\/h3>\n<p>We consider the same example that we took in the static binding. But this code differs from the above code and gives the different output as the actual method overriding is happening here.<\/p>\n<p>The actual overriding takes place since the methods are not declared as static, private, and final. Let\u2019s see an example to understand this:<\/p>\n<p><strong>Code to understand the Dynamic Binding in Java:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">package com.techvidvan.binding\nclass Person\n{\n  public void speak()\n  {\n    System.out.println(\"Person speaks\");\n  }\n}\nclass Teacher extends Person\n{\n  @Override\n  public void speak()\n  {\n    System.out.println(\"Teacher speaks\");\n  }\n}\npublic class DynamicBinding\n{\n  public static void main( String args[])\n  {\n    \/\/Reference and objects are of Person type.\n    Person obj2 = new Person();\n    obj2.speak();\n    \/\/ Reference is of Person type and object is Teacher type\n    Person obj = new Teacher();\n    obj.speak();\n  }\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">Person speaks<br \/>\nTeacher speaks<\/div>\n<p><strong>From the above code, we got different output because:<\/strong><\/p>\n<ul>\n<li>We have not declared the methods as static in the code.<\/li>\n<li>During compilation, the compiler has no idea of which method to call. This happens because the compiler doesn\u2019t go according to the type of the object but it checks only according to the reference variable. Therefore the binding gets delayed to runtime, so the respective version of the speak() method will be called on the basis of the type of the object.<\/li>\n<\/ul>\n<h3>Differences between Static Binding and Dynamic Binding<\/h3>\n<table>\n<tbody>\n<tr>\n<td><b>S.N.<\/b><\/td>\n<td><b>Static Binding<\/b><\/td>\n<td><b>Dynamic Binding<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">\u00a0 1.<\/span><\/td>\n<td><span style=\"font-weight: 400\">A type of polymorphism that collects the information to call a method during the compile-time.<\/span><\/td>\n<td><span style=\"font-weight: 400\">A type of polymorphism that collects the information to call a method at the runtime.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">\u00a0 2.\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400\">The binding happens at compile time.<\/span><\/td>\n<td><span style=\"font-weight: 400\">The binding happens at runtime.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">\u00a0 3.\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400\">The actual object is not used for binding.<\/span><\/td>\n<td><span style=\"font-weight: 400\">The actual object is used for binding.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">\u00a0 4.\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400\">It is also called early binding because binding happens during compilation.<\/span><\/td>\n<td><span style=\"font-weight: 400\">It is also called late binding because binding happens at run time.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">\u00a0 5.<\/span><\/td>\n<td><span style=\"font-weight: 400\">The execution speed is high.<\/span><\/td>\n<td><span style=\"font-weight: 400\">The execution speed is slow.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">\u00a0 6.\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400\">Method overloading is the best example of static binding.<\/span><\/td>\n<td><span style=\"font-weight: 400\">Method overriding is the best example of dynamic binding.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">\u00a0 7.\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400\">The methods which are private, static and final, show static binding because we can not override them.<\/span><\/td>\n<td><span style=\"font-weight: 400\">The methods other private, static and final methods show dynamic binding because overriding is possible with these methods.<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>Summary<\/h3>\n<p>Binding is an integral part of any Object oriented programming language. It is a crucial topic in any programming language and each programmer must know its working as well as its two forms that we discussed in this article.<\/p>\n<p>Binding can be either Static or Dynamic.The form depends on at what time the method call gets bound to the method definition; either compile-time or during the runtime. We discussed both of these types with examples and the control flow.<\/p>\n<p>In the end, we saw the differences between both static and Dynamic Binding in Java.<\/p>\n<p>Thank you for reading our article. If you still have doubts related to Static and Dynamic Binding in Java, do ask in the comment section and our team will help you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Binding means an association of method call to the method definition. The picture below clearly shows what is binding. There are two types of Binding: Static and Dynamic Binding in Java. If the compiler&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":78270,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[183],"tags":[2345,2346,2347,2348,2349,2350,2351],"class_list":["post-77682","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","tag-difference-between-static-binding-and-dynamic-binding","tag-dynamic-binding-in-java","tag-static-and-dynamic-binding-in-java","tag-static-binding-and-dynamic-binding","tag-static-vs-dynamic-binding","tag-what-is-dynamic-binding-in-java","tag-what-is-static-binding"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Static and Dynamic Binding in Java - Differences and Examples - TechVidvan<\/title>\n<meta name=\"description\" content=\"Static and Dynamic Binding in Java - Learn what is Dynamic binding in java and static binding in java with examples. Also see differences between the two.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/static-and-dynamic-binding-in-java-differences-and-examples\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Static and Dynamic Binding in Java - Differences and Examples - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Static and Dynamic Binding in Java - Learn what is Dynamic binding in java and static binding in java with examples. Also see differences between the two.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/static-and-dynamic-binding-in-java-differences-and-examples\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-04-13T04:30:37+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/04\/java-static-vs-dynamic-binding.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"802\" \/>\n\t<meta property=\"og:image:height\" content=\"420\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Static and Dynamic Binding in Java - Differences and Examples - TechVidvan","description":"Static and Dynamic Binding in Java - Learn what is Dynamic binding in java and static binding in java with examples. Also see differences between the two.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/techvidvan.com\/tutorials\/static-and-dynamic-binding-in-java-differences-and-examples\/","og_locale":"en_US","og_type":"article","og_title":"Static and Dynamic Binding in Java - Differences and Examples - TechVidvan","og_description":"Static and Dynamic Binding in Java - Learn what is Dynamic binding in java and static binding in java with examples. Also see differences between the two.","og_url":"https:\/\/techvidvan.com\/tutorials\/static-and-dynamic-binding-in-java-differences-and-examples\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2020-04-13T04:30:37+00:00","og_image":[{"width":802,"height":420,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/04\/java-static-vs-dynamic-binding.jpg","type":"image\/jpeg"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/static-and-dynamic-binding-in-java-differences-and-examples\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/static-and-dynamic-binding-in-java-differences-and-examples\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Static and Dynamic Binding in Java &#8211; Differences and Examples","datePublished":"2020-04-13T04:30:37+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/static-and-dynamic-binding-in-java-differences-and-examples\/"},"wordCount":1027,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/static-and-dynamic-binding-in-java-differences-and-examples\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/04\/java-static-vs-dynamic-binding.jpg","keywords":["difference between static binding and dynamic binding","dynamic binding in java","static and dynamic binding in java","static binding and dynamic binding","static Vs dynamic binding","what is dynamic binding in java","what is static binding"],"articleSection":["Java Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/static-and-dynamic-binding-in-java-differences-and-examples\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/static-and-dynamic-binding-in-java-differences-and-examples\/","url":"https:\/\/techvidvan.com\/tutorials\/static-and-dynamic-binding-in-java-differences-and-examples\/","name":"Static and Dynamic Binding in Java - Differences and Examples - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/static-and-dynamic-binding-in-java-differences-and-examples\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/static-and-dynamic-binding-in-java-differences-and-examples\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/04\/java-static-vs-dynamic-binding.jpg","datePublished":"2020-04-13T04:30:37+00:00","description":"Static and Dynamic Binding in Java - Learn what is Dynamic binding in java and static binding in java with examples. Also see differences between the two.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/static-and-dynamic-binding-in-java-differences-and-examples\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/static-and-dynamic-binding-in-java-differences-and-examples\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/static-and-dynamic-binding-in-java-differences-and-examples\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/04\/java-static-vs-dynamic-binding.jpg","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/04\/java-static-vs-dynamic-binding.jpg","width":802,"height":420,"caption":"Static and dynamic binding in java"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/static-and-dynamic-binding-in-java-differences-and-examples\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Static and Dynamic Binding in Java &#8211; Differences and Examples"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/77682","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=77682"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/77682\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/78270"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=77682"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=77682"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=77682"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}