{"id":3190,"date":"2017-04-01T18:36:38","date_gmt":"2017-04-01T13:06:38","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=3190"},"modified":"2017-04-01T18:36:38","modified_gmt":"2017-04-01T13:06:38","slug":"call-one-constructor-another-java","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/call-one-constructor-another-java\/","title":{"rendered":"JAVA &#8211; How do I call one constructor from another in Java?"},"content":{"rendered":"<ul>\n<li>In Java another constructor of the same class can be called from a constructor via this() . Note however that this has to be on the first line. Within a constructor, we can use the this keyword to invoke another constructor in the same class. Doing so is called an explicit constructor invocation.<\/li>\n<\/ul>\n<h4 id=\"constructor-chaining-in-java\"><span style=\"color: #800000;\">Constructor Chaining in Java:<\/span><\/h4>\n<ul>\n<li>In Java, we can call one constructor from another and it\u2019s known as constructor chaining in Java.<\/li>\n<li>this and super keyword is used to call one constructor from other in Java.<\/li>\n<li>this() can be used to call another constructor of same class while super() can be used to call a constructor from super class in Java.<\/li>\n<li>this() in reality calls no argument constructor of the same class while this(2) calls another constructor of the same class which accepts one integer parameter.<\/li>\n<li>super() can be used to call no argument constructor of super class and super with parameter can be used to call other overloaded constructors of parent class.<\/li>\n<li>Calling one constructor from other is called constructor chaining in Java<\/li>\n<li>Constructor chaining is also used to implement telescoping pattern where an object can be created with combination of multiple property.<\/li>\n<\/ul>\n<p><strong>Example Program<\/strong><\/p>\n<ul>\n<li>In the below example the class \u201cChainingDemo\u201d has 4 constructors and we are calling one constructor from another using this() statement.<\/li>\n<li>For e.g. in order to call a constructor with single string argument we have supplied a string in this() statement like this(\u201chello\u201d).<\/li>\n<\/ul>\n<p>Note: this() should always be the first statement in constructor otherwise you will get the below error message:<\/p>\n<p>Exception in thread &#8220;main&#8221; java.lang.Error: Unresolved compilation problem:<\/p>\n<ul>\n<li>Constructor call must be the first statement in a constructor<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Java Code<\/span> <\/div> <pre class=\"language-java code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-java code-embed-code\">package kaashivwiki.com;<br\/>public class ChainingDemo {<br\/>   \/\/default constructor of the class<br\/>   public ChainingDemo(){<br\/>         System.out.println(&quot;Default constructor&quot;);<br\/>   }<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Java Code<\/span> <\/div> <pre class=\"language-java code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-java code-embed-code\">public ChainingDemo(String str){<br\/>         this();<br\/>    \t System.out.println(&quot;Parametrized constructor with single param&quot;);<br\/>   }<br\/>   public ChainingDemo(String str, int num){<br\/>         \/\/It will call the constructor with String argument<br\/>    \t this(&quot;Hello&quot;); <br\/>    \t System.out.println(&quot;Parametrized constructor with double args&quot;);<br\/>   }<br\/>   public ChainingDemo(int num1, int num2, int num3){<br\/>   \t\/\/ It will call the constructor with (String, integer) arguments<br\/>        this(&quot;Hello&quot;, 2);<br\/>        System.out.println(&quot;Parametrized constructor with three args&quot;);<br\/>   }<br\/>   public static void main(String args[]){<br\/>        \/\/Creating an object using Constructor with 3 int arguments<br\/>        ChainingDemo obj = new ChainingDemo(5,5,15);<br\/>   }<br\/>}<\/code><\/pre> <\/div>\n<h4 id=\"output\"><span style=\"color: #ff6600;\">Output:<\/span><\/h4>\n<p>Default constructor<br \/>\nParametrized constructor with single param<br \/>\nParametrized constructor with double args<br \/>\nParametrized constructor with three args<\/p>\n<p><strong>Another example of constructor chaining which shows How to call overloaded constructor of same class and parent class in Java.<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Java Code<\/span> <\/div> <pre class=\"language-java code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-java code-embed-code\">public class ConstructorChainingExample {<br\/>  <br\/>  <br\/>    public static void main(String args[]) {<br\/>    <br\/>        \/\/this will first call one argument constructor of Child Class which<br\/>        \/\/in turn call corresponding constructor of super class using super(String)<br\/>        System.out.println(&quot;Constructor chaining Example in Java&quot;);<br\/>        Child child = new Child(&quot;Jeremy&quot;);<br\/>      <br\/>        \/\/this constructor will call no argument constructor of Child,<br\/>        \/\/which then call one argument constructor of<br\/>        \/\/same class, which finally call corresponding one argument constructor<br\/>        \/\/ of super class Parent.<br\/>        System.out.println(&quot;---------------------------------&quot;);<br\/>        Child emptyChild = new Child();<br\/>    } <br\/>  <br\/>}<\/code><\/pre> <\/div>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Java Code<\/span> <\/div> <pre class=\"language-java code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-java code-embed-code\">class Parent{<br\/>    private String name;<br\/>    \/* * Calling constructor of same class with one String argument*\/<br\/>    protected Parent(){<br\/>        this(&quot;&quot;);<br\/>        System.out.println(&quot;No argument constructor of Parent called &quot;);<br\/>    }<br\/>  <br\/>    protected Parent(String name){<br\/>        this.name = name;<br\/>        System.out.println(&quot;One String argument constructor of Parent called &quot;);<br\/>    }<br\/>}<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Java Code<\/span> <\/div> <pre class=\"language-java code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-java code-embed-code\">class Child extends Parent{<br\/>    private String name;<br\/>  <br\/>    \/** Calling constructor same class with one argument *\/<br\/>    protected Child(){<br\/>        this(&quot;&quot;);<br\/>        System.out.println(&quot;No argument constructor of Child called &quot;);<br\/>    }<\/code><\/pre> <\/div>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Java Code<\/span> <\/div> <pre class=\"language-java code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-java code-embed-code\">\/*\t* Calling constructor of super class with one argument<br\/>     \t* call to super() must be first line in constructor<br\/>     \t*\/<br\/>    protected Child(String name){<br\/>        super(name);<br\/>        System.out.println(&quot;One argument constructor of Super class called from sub class &quot;);<br\/>    }<br\/>}<\/code><\/pre> <\/div>\n<h4 id=\"output-2\"><strong><span style=\"color: #808000;\">OUTPUT:<\/span><\/strong><\/h4>\n<p>Constructor chaining Example in Java<br \/>\nOne String argument constructor of Parent called<br \/>\nOne argument constructor of Super class called from sub class<br \/>\n&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br \/>\nOne String argument constructor of Parent called<br \/>\nOne argument constructor of Super class called from sub class<br \/>\nNo argument constructor of Child called<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How do I call one constructor from another in Java? In Java another constructor of the same class can be called from a constructor via this().<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2139],"tags":[5978,5981,5971,5979,5973,5982,5970,5975,5969,5974,5968,5983,5972,5976,5977,5980],"class_list":["post-3190","post","type-post","status-publish","format-standard","hentry","category-java","tag-call-constructor-from-constructor-in-c-can-i-call-an-overloaded-constructor-from-another-constructor-of-the-same-class-in-c","tag-call-one-constructor-from-another","tag-call-one-constructor-from-another-c","tag-call-one-constructor-from-another-in-java","tag-call-to-this-must-be-first-statement-in-constructor","tag-calling-a-variable-from-constructor-in-another-method-in-java","tag-calling-one-constructor-from-another-c","tag-calling-the-base-constructor-in-c","tag-chaining-constructors-java","tag-constructor-chaining-in-inheritance-in-java-how-to-call-constructor-of-one-class-in-another-class","tag-how-to-call-a-constructor-from-another-class-in-java","tag-java-call-a-constructor-from-another-constructor-without-immediately-having-the-parameters","tag-java-constructor-call-another-constructor-not-first-line","tag-virtual-member-call-in-a-constructor","tag-what-are-the-rules-for-calling-the-superclass-constructor","tag-whats-wrong-with-overridable-method-calls-in-constructors"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/3190","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=3190"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/3190\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=3190"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=3190"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=3190"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}