{"id":37,"date":"2019-01-15T02:22:35","date_gmt":"2019-01-15T02:22:35","guid":{"rendered":"https:\/\/kindsonthegenius.com\/java\/?p=37"},"modified":"2019-03-02T04:35:15","modified_gmt":"2019-03-02T04:35:15","slug":"06-java-constructors","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/java\/06-java-constructors\/","title":{"rendered":"Java &#8211; Constructors"},"content":{"rendered":"\n<ol class=\"wp-block-list\"><li><a href=\"#t1\">Introduction<\/a><\/li><li><a href=\"#t2\">Constructor with no Arguments<\/a><\/li><li><a href=\"#t3\">Parameterized  Constructors<\/a><\/li><li><a href=\"#t4\">Creating new Objects<\/a><\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"t1\">1. Introduction<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Constructors in Java are  what we use to create new objects. Furthermore, you use a constructor on initialize new objects.  Constructors have the same name as the class and not return type.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Generally,  you assign initial values to instance variables using a constructor. But you can also perform some initial tasks required when a new object is created.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The code below give an example of a constructor.<\/p>\n\n\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\"><pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #008800; font-weight: bold\">public<\/span> <span style=\"color: #008800; font-weight: bold\">class<\/span> <span style=\"color: #BB0066; font-weight: bold\">Vehicle<\/span> <span style=\"color: #333333\">{<\/span>\n\t<span style=\"color: #008800; font-weight: bold\">public<\/span> String make<span style=\"color: #333333\">;<\/span>\n\t\n\t<span style=\"color: #008800; font-weight: bold\">public<\/span> <span style=\"color: #0066BB; font-weight: bold\">Vehicle<\/span><span style=\"color: #333333\">(<\/span>String make<span style=\"color: #333333\">)<\/span> <span style=\"color: #333333\">{<\/span> <span style=\"color: #888888\">\/\/constructor with one parameter<\/span>\n\t\t<span style=\"color: #008800; font-weight: bold\">this<\/span><span style=\"color: #333333\">.<\/span><span style=\"color: #0000CC\">make<\/span> <span style=\"color: #333333\">=<\/span> make<span style=\"color: #333333\">;<\/span>\n\t<span style=\"color: #333333\">}<\/span>\n<span style=\"color: #333333\">}<\/span>\n<\/pre>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">We have two types of constructors in Java.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Constructor with no arguments<\/li><li>Parameterized constructor<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong id=\"t2\">2. Constructors with no arguments<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">These constructors require not arguments. In this way you can create an object even if you don&#8217;t have the attributes. Take for example, the code below.<\/p>\n\n\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\"><pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #008800; font-weight: bold\">public<\/span> <span style=\"color: #008800; font-weight: bold\">class<\/span> <span style=\"color: #BB0066; font-weight: bold\">Circle<\/span> <span style=\"color: #333333\">{<\/span>\n\t<span style=\"color: #333399; font-weight: bold\">int<\/span> radius<span style=\"color: #333333\">;<\/span>\n\t\n\t<span style=\"color: #008800; font-weight: bold\">public<\/span> <span style=\"color: #0066BB; font-weight: bold\">Circle<\/span> <span style=\"color: #333333\">()<\/span> <span style=\"color: #333333\">{<\/span> <span style=\"color: #888888\">\/\/Constructor with no parameter<\/span>\n\t\tradius <span style=\"color: #333333\">=<\/span> <span style=\"color: #0000DD; font-weight: bold\">50<\/span><span style=\"color: #333333\">;<\/span>\n\t<span style=\"color: #333333\">}<\/span>\n<span style=\"color: #333333\">}<\/span>\n<\/pre>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">The constructor for circle has no parameters. However, when a new circle is created, it automatically has a radius of 50. Therefore if you run the code below, you will have an output of 50.<\/p>\n\n\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\"><pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #008800; font-weight: bold\">public<\/span> <span style=\"color: #008800; font-weight: bold\">class<\/span> <span style=\"color: #BB0066; font-weight: bold\">Tester<\/span> <span style=\"color: #333333\">{<\/span>\n\n\t<span style=\"color: #008800; font-weight: bold\">public<\/span> <span style=\"color: #008800; font-weight: bold\">static<\/span> <span style=\"color: #333399; font-weight: bold\">void<\/span> <span style=\"color: #0066BB; font-weight: bold\">main<\/span><span style=\"color: #333333\">(<\/span>String<span style=\"color: #333333\">[]<\/span> args<span style=\"color: #333333\">)<\/span> <span style=\"color: #333333\">{<\/span>\n\t\tCircle circle <span style=\"color: #333333\">=<\/span> <span style=\"color: #008800; font-weight: bold\">new<\/span> Circle<span style=\"color: #333333\">();<\/span>\n\t\t\n\t\tSystem<span style=\"color: #333333\">.<\/span><span style=\"color: #0000CC\">out<\/span><span style=\"color: #333333\">.<\/span><span style=\"color: #0000CC\">println<\/span><span style=\"color: #333333\">(<\/span>circle<span style=\"color: #333333\">.<\/span><span style=\"color: #0000CC\">radius<\/span><span style=\"color: #333333\">);<\/span>\n\t<span style=\"color: #333333\">}<\/span>\n<span style=\"color: #333333\">}<\/span>\n<\/pre>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong id=\"t3\">3. Parameterized Constructors<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Sometimes, you will need a constructor that accepts one or more parameters. You specify the parameter inside braces after the constructor name. An example is given below<\/p>\n\n\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\"><pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #008800; font-weight: bold\">public<\/span> <span style=\"color: #008800; font-weight: bold\">class<\/span> <span style=\"color: #BB0066; font-weight: bold\">Circle<\/span> <span style=\"color: #333333\">{<\/span>\n\t<span style=\"color: #008800; font-weight: bold\">public<\/span> <span style=\"color: #333399; font-weight: bold\">int<\/span> radius<span style=\"color: #333333\">;<\/span>\n\t\n\t<span style=\"color: #008800; font-weight: bold\">public<\/span> <span style=\"color: #0066BB; font-weight: bold\">Circle<\/span> <span style=\"color: #333333\">(<\/span><span style=\"color: #333399; font-weight: bold\">int<\/span> radius<span style=\"color: #333333\">)<\/span> <span style=\"color: #333333\">{<\/span> <span style=\"color: #888888\">\/\/Parameterized constructor<\/span>\n\t\t<span style=\"color: #008800; font-weight: bold\">this<\/span><span style=\"color: #333333\">.<\/span><span style=\"color: #0000CC\">radius<\/span> <span style=\"color: #333333\">=<\/span> radius<span style=\"color: #333333\">;<\/span>\n\t<span style=\"color: #333333\">}<\/span>\n<span style=\"color: #333333\">}<\/span>\n<\/pre>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">In the same way, you can run create a circle using the code below. <\/p>\n\n\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\"><pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #008800; font-weight: bold\">public<\/span> <span style=\"color: #008800; font-weight: bold\">class<\/span> <span style=\"color: #BB0066; font-weight: bold\">Tester<\/span> <span style=\"color: #333333\">{<\/span>\n\n\t<span style=\"color: #008800; font-weight: bold\">public<\/span> <span style=\"color: #008800; font-weight: bold\">static<\/span> <span style=\"color: #333399; font-weight: bold\">void<\/span> <span style=\"color: #0066BB; font-weight: bold\">main<\/span><span style=\"color: #333333\">(<\/span>String<span style=\"color: #333333\">[]<\/span> args<span style=\"color: #333333\">)<\/span> <span style=\"color: #333333\">{<\/span>\n\t\tCircle circle <span style=\"color: #333333\">=<\/span> <span style=\"color: #008800; font-weight: bold\">new<\/span> Circle<span style=\"color: #333333\">(<\/span><span style=\"color: #0000DD; font-weight: bold\">50<\/span><span style=\"color: #333333\">);<\/span>\n\t\t\n\t\tSystem<span style=\"color: #333333\">.<\/span><span style=\"color: #0000CC\">out<\/span><span style=\"color: #333333\">.<\/span><span style=\"color: #0000CC\">println<\/span><span style=\"color: #333333\">(<\/span>circle<span style=\"color: #333333\">.<\/span><span style=\"color: #0000CC\">radius<\/span><span style=\"color: #333333\">);<\/span>\n\t<span style=\"color: #333333\">}<\/span>\n<span style=\"color: #333333\">}<\/span>\n<\/pre>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">Running the above code would also produce similar result to the previous one. 50 is printed out. But notice that in this constructor, you pass 50 as a parameter.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Finally bear in mind, that two or even more constructors can be in the same class.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong id=\"t4\">4. Creating New Objects<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Just as you know, objects are create from classes. You use the new keyword to create new objects from classes. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Note the three steps involved in creating new objects<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Declaration<\/strong>: Here, you specify a variable name as an object type<\/li><li><strong>Instantiation<\/strong>: You use the new keyword<\/li><li><strong>Initialization<\/strong>: Assign initial values<\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Introduction Constructor with no Arguments Parameterized Constructors Creating new Objects 1. Introduction Constructors in Java are what we use to create new objects. Furthermore, you &hellip; <\/p>\n","protected":false},"author":395,"featured_media":38,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[2],"tags":[],"class_list":["post-37","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java-tutorial"],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/java\/wp-json\/wp\/v2\/posts\/37","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/java\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/java\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/java\/wp-json\/wp\/v2\/users\/395"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/java\/wp-json\/wp\/v2\/comments?post=37"}],"version-history":[{"count":1,"href":"https:\/\/kindsonthegenius.com\/java\/wp-json\/wp\/v2\/posts\/37\/revisions"}],"predecessor-version":[{"id":39,"href":"https:\/\/kindsonthegenius.com\/java\/wp-json\/wp\/v2\/posts\/37\/revisions\/39"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/java\/wp-json\/wp\/v2\/media\/38"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/java\/wp-json\/wp\/v2\/media?parent=37"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/java\/wp-json\/wp\/v2\/categories?post=37"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/java\/wp-json\/wp\/v2\/tags?post=37"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}