{"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 \u201cmain\u201d 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[pastacode lang=\u201djava\u201d manual=\u201dpackage%20kaashivwiki.com%3B%0Apublic%20class%20ChainingDemo%20%7B%0A%20%20%20%2F%2Fdefault%20constructor%20of%20the%20class%0A%20%20%20public%20ChainingDemo()%7B%0A%20%20%20%20%20%20%20%20%20System.out.println(%22Default%20constructor%22)%3B%0A%20%20%20%7D\u201d message=\u201dJava Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n[pastacode lang=\u201djava\u201d manual=\u201dpublic%20ChainingDemo(String%20str)%7B%0A%20%20%20%20%20%20%20%20%20this()%3B%0A%20%20%20%20%09%20System.out.println(%22Parametrized%20constructor%20with%20single%20param%22)%3B%0A%20%20%20%7D%0A%20%20%20public%20ChainingDemo(String%20str%2C%20int%20num)%7B%0A%20%20%20%20%20%20%20%20%20%2F%2FIt%20will%20call%20the%20constructor%20with%20String%20argument%0A%20%20%20%20%09%20this(%22Hello%22)%3B%20%0A%20%20%20%20%09%20System.out.println(%22Parametrized%20constructor%20with%20double%20args%22)%3B%0A%20%20%20%7D%0A%20%20%20public%20ChainingDemo(int%20num1%2C%20int%20num2%2C%20int%20num3)%7B%0A%20%20%20%09%2F%2F%20It%20will%20call%20the%20constructor%20with%20(String%2C%20integer)%20arguments%0A%20%20%20%20%20%20%20%20this(%22Hello%22%2C%202)%3B%0A%20%20%20%20%20%20%20%20System.out.println(%22Parametrized%20constructor%20with%20three%20args%22)%3B%0A%20%20%20%7D%0A%20%20%20public%20static%20void%20main(String%20args%5B%5D)%7B%0A%20%20%20%20%20%20%20%20%2F%2FCreating%20an%20object%20using%20Constructor%20with%203%20int%20arguments%0A%20%20%20%20%20%20%20%20ChainingDemo%20obj%20%3D%20new%20ChainingDemo(5%2C5%2C15)%3B%0A%20%20%20%7D%0A%7D\u201d message=\u201dJava Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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[pastacode lang=\u201djava\u201d manual=\u201dpublic%20class%20ConstructorChainingExample%20%7B%0A%20%20%0A%20%20%0A%20%20%20%20public%20static%20void%20main(String%20args%5B%5D)%20%7B%0A%20%20%20%20%0A%20%20%20%20%20%20%20%20%2F%2Fthis%20will%20first%20call%20one%20argument%20constructor%20of%20Child%20Class%20which%0A%20%20%20%20%20%20%20%20%2F%2Fin%20turn%20call%20corresponding%20constructor%20of%20super%20class%20using%20super(String)%0A%20%20%20%20%20%20%20%20System.out.println(%22Constructor%20chaining%20Example%20in%20Java%22)%3B%0A%20%20%20%20%20%20%20%20Child%20child%20%3D%20new%20Child(%22Jeremy%22)%3B%0A%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%2F%2Fthis%20constructor%20will%20call%20no%20argument%20constructor%20of%20Child%2C%0A%20%20%20%20%20%20%20%20%2F%2Fwhich%20then%20call%20one%20argument%20constructor%20of%0A%20%20%20%20%20%20%20%20%2F%2Fsame%20class%2C%20which%20finally%20call%20corresponding%20one%20argument%20constructor%0A%20%20%20%20%20%20%20%20%2F%2F%20of%20super%20class%20Parent.%0A%20%20%20%20%20%20%20%20System.out.println(%22\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014%22)%3B%0A%20%20%20%20%20%20%20%20Child%20emptyChild%20%3D%20new%20Child()%3B%0A%20%20%20%20%7D%20%0A%20%20%0A%7D\u201d message=\u201dJava Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[pastacode lang=\u201djava\u201d manual=\u201dclass%20Parent%7B%0A%20%20%20%20private%20String%20name%3B%0A%20%20%20%20%2F*%20*%20Calling%20constructor%20of%20same%20class%20with%20one%20String%20argument*%2F%0A%20%20%20%20protected%20Parent()%7B%0A%20%20%20%20%20%20%20%20this(%22%22)%3B%0A%20%20%20%20%20%20%20%20System.out.println(%22No%20argument%20constructor%20of%20Parent%20called%20%22)%3B%0A%20%20%20%20%7D%0A%20%20%0A%20%20%20%20protected%20Parent(String%20name)%7B%0A%20%20%20%20%20%20%20%20this.name%20%3D%20name%3B%0A%20%20%20%20%20%20%20%20System.out.println(%22One%20String%20argument%20constructor%20of%20Parent%20called%20%22)%3B%0A%20%20%20%20%7D%0A%7D\u201d message=\u201dJava Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n[pastacode lang=\u201djava\u201d manual=\u201dclass%20Child%20extends%20Parent%7B%0A%20%20%20%20private%20String%20name%3B%0A%20%20%0A%20%20%20%20%2F**%20Calling%20constructor%20same%20class%20with%20one%20argument%20*%2F%0A%20%20%20%20protected%20Child()%7B%0A%20%20%20%20%20%20%20%20this(%22%22)%3B%0A%20%20%20%20%20%20%20%20System.out.println(%22No%20argument%20constructor%20of%20Child%20called%20%22)%3B%0A%20%20%20%20%7D\u201d message=\u201dJava Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[pastacode lang=\u201djava\u201d manual=\u201d%2F*%09*%20Calling%20constructor%20of%20super%20class%20with%20one%20argument%0A%20%20%20%20%20%09*%20call%20to%20super()%20must%20be%20first%20line%20in%20constructor%0A%20%20%20%20%20%09*%2F%0A%20%20%20%20protected%20Child(String%20name)%7B%0A%20%20%20%20%20%20%20%20super(name)%3B%0A%20%20%20%20%20%20%20%20System.out.println(%22One%20argument%20constructor%20of%20Super%20class%20called%20from%20sub%20class%20%22)%3B%0A%20%20%20%20%7D%0A%7D\u201d message=\u201dJava Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014<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}]}}