{"id":1090,"date":"2022-02-07T18:48:09","date_gmt":"2022-02-07T18:48:09","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=1090"},"modified":"2022-02-07T18:48:09","modified_gmt":"2022-02-07T18:48:09","slug":"constructors","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/02\/07\/constructors\/","title":{"rendered":"Constructors"},"content":{"rendered":"\n<p>A constructor initializes an object when it is created. It has the same name as its class and is syntactically similar to a method. However, constructors have no explicit return type.<\/p>\n\n\n\n<p>Typically, you will use a constructor to give initial values to the instance variables defined by the class, or to perform any other start-up procedures required to create a fully formed object.<\/p>\n\n\n\n<p>All classes have constructors, whether you define one or not, because Java automatically provides a default constructor that initializes all member variables to zero. However, once you define your own constructor, the default constructor is no longer used.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"syntax\">Syntax<\/h2>\n\n\n\n<p>Following is the syntax of a constructor \u2212<\/p>\n\n\n\n<pre id=\"constructor1\" class=\"wp-block-code\"><code>class ClassName {\n   ClassName() {\n   }\n}<\/code><\/pre>\n\n\n\n<p>Java allows two types of constructors namely \u2212<\/p>\n\n\n\n<ul><li>No argument Constructors<\/li><li>Parameterized Constructors<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"no-argument-constructors\">No argument Constructors<\/h2>\n\n\n\n<p>As the name specifies the no argument constructors of Java does not accept any parameters instead, using these constructors the instance variables of a method will be initialized with fixed values for all objects.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"example\">Example<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>Public class MyClass {\n   Int num;\n   MyClass() {\n      num = 100;\n   }\n}<\/code><\/pre>\n\n\n\n<p>You would call constructor to initialize objects as follows<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class ConsDemo {\n   public static void main(String args&#91;]) {\n      MyClass t1 = new MyClass();\n      MyClass t2 = new MyClass();\n      System.out.println(t1.num + \" \" + t2.num);\n   }\n}<\/code><\/pre>\n\n\n\n<p>This would produce the following result<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">100 100\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"parameterized-constructors\">Parameterized Constructors<\/h2>\n\n\n\n<p>Most often, you will need a constructor that accepts one or more parameters. Parameters are added to a constructor in the same way that they are added to a method, just declare them inside the parentheses after the constructor&#8217;s name.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"example\">Example<\/h2>\n\n\n\n<p>Here is a simple example that uses a constructor \u2212<\/p>\n\n\n\n<pre id=\"constructor2\" class=\"wp-block-code\"><code>\/\/ A simple constructor.\nclass MyClass {\n   int x;\n   \n   \/\/ Following is the constructor\n   MyClass(int i ) {\n      x = i;\n   }\n}<\/code><\/pre>\n\n\n\n<p>You would call constructor to initialize objects as follows \u2212<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class ConsDemo {\n   public static void main(String args&#91;]) {\n      MyClass t1 = new MyClass( 10 );\n      MyClass t2 = new MyClass( 20 );\n      System.out.println(t1.x + \" \" + t2.x);\n   }\n}<\/code><\/pre>\n\n\n\n<p>This would produce the following result \u2212<\/p>\n\n\n\n<pre id=\"block-6b2e2943-4de9-4eae-8633-534d98cc8899\" class=\"wp-block-preformatted\">10 20<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>A constructor initializes an object when it is created. It has the same name as its class and is syntactically similar to a method. However, constructors have no explicit return type. Typically, you will use a constructor to give initial values to the instance variables defined by the class, or to perform any other start-up [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[238],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1090"}],"collection":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/comments?post=1090"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1090\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=1090"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=1090"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=1090"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}