{"id":1187,"date":"2022-02-24T20:12:36","date_gmt":"2022-02-24T20:12:36","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=1187"},"modified":"2022-02-24T20:12:36","modified_gmt":"2022-02-24T20:12:36","slug":"java-constructors","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/02\/24\/java-constructors\/","title":{"rendered":"Java Constructors"},"content":{"rendered":"\n<p>In this tutorial, we will learn about Java constructors, their types, and how to use them with the help of examples.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"introduction\">What is a Constructor?<\/h2>\n\n\n\n<p>A constructor in Java is similar to a method that is invoked when an object of the class is created.<\/p>\n\n\n\n<p>Unlike\u00a0Java methods, a constructor has the same name as that of the class and does not have any return type. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Test {\n  Test() {\n    \/\/ constructor body\n  }\n}<\/code><\/pre>\n\n\n\n<p>Here,&nbsp;<code>Test()<\/code>&nbsp;is a constructor. It has the same name as that of the class and doesn&#8217;t have a return type.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"example\">Example 1: Java Constructor<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>class Main {\n  private String name;\n\n  \/\/ constructor\n  Main() {\n    System.out.println(\"Constructor Called:\");\n    name = \"Programiz\";\n  }\n\n  public static void main(String&#91;] args) {\n\n    \/\/ constructor is invoked while\n    \/\/ creating an object of the Main class\n    Main obj = new Main();\n    System.out.println(\"The name is \" + obj.name);\n  }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>Constructor Called:\nThe name is Programiz<\/samp><\/code><\/pre>\n\n\n\n<p>In the above example, we have created a constructor named&nbsp;<code>Main()<\/code>. Inside the constructor, we are initializing the value of the&nbsp;<var>name<\/var>&nbsp;variable.<\/p>\n\n\n\n<p>Notice the statement of creating an object of the&nbsp;<var>Main<\/var>&nbsp;class.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Main obj = new Main();<\/code><\/pre>\n\n\n\n<p>Here, when the object is created, the&nbsp;<code>Main()<\/code>&nbsp;constructor is called. And, the value of the&nbsp;<var>name<\/var>&nbsp;variable is initialized.<\/p>\n\n\n\n<p>Hence, the program prints the value of the&nbsp;<var>name<\/var>&nbsp;variables as&nbsp;<code>Programiz<\/code>.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Types of Constructor<\/h3>\n\n\n\n<p>In Java, constructors can be divided into 3 types:<\/p>\n\n\n\n<ol><li>No-Arg Constructor<\/li><li>Parameterized Constructor<\/li><li>Default Constructor<\/li><\/ol>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"no-arg\">1. Java No-Arg Constructors<\/h2>\n\n\n\n<p>Similar to methods, a Java constructor may or may not have any parameters (arguments).<\/p>\n\n\n\n<p>If a constructor does not accept any parameters, it is known as a no-argument constructor. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>private Constructor() {\n   \/\/ body of the constructor\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Example 2: Java private no-arg constructor<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class Main {\n\n  int i;\n\n  \/\/ constructor with no parameter\n  private Main() {\n    i = 5;\n    System.out.println(\"Constructor is called\");\n  }\n\n  public static void main(String&#91;] args) {\n\n    \/\/ calling the constructor without any parameter\n    Main obj = new Main();\n    System.out.println(\"Value of i: \" + obj.i);\n  }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>Constructor is called\nValue of i: 5<\/samp><\/code><\/pre>\n\n\n\n<p>In the above example, we have created a constructor&nbsp;<code>Main()<\/code>. Here, the constructor does not accept any parameters. Hence, it is known as a no-arg constructor.<\/p>\n\n\n\n<p><strong>Notice that we have declared the constructor as private.<\/strong><\/p>\n\n\n\n<p>Once a constructor is declared&nbsp;<code>private<\/code>, it cannot be accessed from outside the class. So, creating objects from outside the class is prohibited using the private constructor.<\/p>\n\n\n\n<p>Here, we are creating the object inside the same class. Hence, the program is able to access the constructor. <\/p>\n\n\n\n<p>However, if we want to create objects outside the class, then we need to declare the constructor as&nbsp;<code>public<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example 3: Java public no-arg constructors<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class Company {\n  String name;\n\n  \/\/ public constructor\n  public Company() {\n    name = \"Programiz\";\n  }\n}\n\nclass Main {\n  public static void main(String&#91;] args) {\n\n    \/\/ object is created in another class\n    Company obj = new Company();\n    System.out.println(\"Company name = \" + obj.name);\n  }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>Company name = Programiz<\/samp><\/code><\/pre>\n\n\n\n<p><strong>Recommended Reading:<\/strong>\u00a0Java Access Modifier<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"parameterized\">2. Java Parameterized Constructor<\/h2>\n\n\n\n<p>A Java constructor can also accept one or more parameters. Such constructors are known as parameterized constructors (constructor with parameters).<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example 4: Parameterized constructor<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class Main {\n\n  String languages;\n\n  \/\/ constructor accepting single value\n  Main(String lang) {\n    languages = lang;\n    System.out.println(languages + \" Programming Language\");\n  }\n\n  public static void main(String&#91;] args) {\n\n    \/\/ call constructor by passing a single value\n    Main obj1 = new Main(\"Java\");\n    Main obj2 = new Main(\"Python\");\n    Main obj3 = new Main(\"C\");\n  }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>Java Programming Language\nPython Programming Language\nC Programming Language<\/samp><\/code><\/pre>\n\n\n\n<p>In the above example, we have created a constructor named&nbsp;<code>Main()<\/code>. Here, the constructor takes a single parameter. Notice the expression,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Main obj1 = new Main(\"Java\");<\/code><\/pre>\n\n\n\n<p>Here, we are passing the single value to the constructor. Based on the argument passed, the language variable is initialized inside the constructor.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"default\">3. Java Default Constructor<\/h2>\n\n\n\n<p>If we do not create any constructor, the Java compiler automatically create a no-arg constructor during the execution of the program. This constructor is called default constructor.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example 5: Default Constructor<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class Main {\n\n  int a;\n  boolean b;\n\n  public static void main(String&#91;] args) {\n\n    \/\/ A default constructor is called\n    Main obj = new Main();\n\n    System.out.println(\"Default Value:\");\n    System.out.println(\"a = \" + obj.a);\n    System.out.println(\"b = \" + obj.b);\n  }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>a = 0\nb = false<\/samp><\/code><\/pre>\n\n\n\n<p>Here, we haven&#8217;t created any constructors. Hence, the Java compiler automatically creates the default constructor.<\/p>\n\n\n\n<p>The default constructor initializes any uninitialized instance variables with default values.<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-regular\"><table><tbody><tr><th>Type<\/th><th>Default Value<\/th><\/tr><tr><td><code>boolean<\/code><\/td><td>false<\/td><\/tr><tr><td><code>byte<\/code><\/td><td>0<\/td><\/tr><tr><td><code>short<\/code><\/td><td>0<\/td><\/tr><tr><td><code>int<\/code><\/td><td>0<\/td><\/tr><tr><td><code>long<\/code><\/td><td>0L<\/td><\/tr><tr><td><code>char<\/code><\/td><td>\\u0000<\/td><\/tr><tr><td><code>float<\/code><\/td><td>0.0f<\/td><\/tr><tr><td><code>double<\/code><\/td><td>0.0d<\/td><\/tr><tr><td><code>object<\/code><\/td><td>Reference null<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>In the above program, the variables&nbsp;<var>a<\/var>&nbsp;and&nbsp;<var>b<\/var>&nbsp;are initialized with default value&nbsp;<strong>0<\/strong>&nbsp;and&nbsp;<code>false<\/code>&nbsp;respectively.<\/p>\n\n\n\n<p>The above program is equivalent to:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Main {\n\n  int a;\n  boolean b;\n\n  \/\/ a private constructor\n  private Main() {\n    a = 0;\n    b = false;\n  }\n\n  public static void main(String&#91;] args) {\n    \/\/ call the constructor\n    Main obj = new Main();\n\n    System.out.println(\"Default Value:\");\n    System.out.println(\"a = \" + obj.a);\n    System.out.println(\"b = \" + obj.b);\n  }\n}<\/code><\/pre>\n\n\n\n<p><strong>The output of the program is the same as Example 5.<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"notes\">Important Notes on Java Constructors<\/h2>\n\n\n\n<ul><li>Constructors are invoked implicitly when you instantiate objects.<\/li><li>The two rules for creating a constructor are:<br>The name of the constructor should be the same as the class.<br>A Java constructor must not have a return type.<\/li><li>If a class doesn&#8217;t have a constructor, the Java compiler automatically creates a&nbsp;<strong>default constructor<\/strong>&nbsp;during run-time. The default constructor initializes instance variables with default values. For example, the&nbsp;<code>int<\/code>&nbsp;variable will be initialized to&nbsp;<code>0<\/code><\/li><li>Constructor types:<br><strong>No-Arg Constructor<\/strong>&nbsp;&#8211; a constructor that does not accept any arguments<br><strong>Parameterized constructor<\/strong>&nbsp;&#8211; a constructor that accepts arguments<br><strong>Default Constructor<\/strong>&nbsp;&#8211; a constructor that is automatically created by the Java compiler if it is not explicitly defined.<\/li><li>A constructor cannot be&nbsp;<code>abstract<\/code>&nbsp;or&nbsp;<code>static<\/code>&nbsp;or&nbsp;<code>final<\/code>.<\/li><li>A constructor can be overloaded but can not be overridden.<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"overloading\">Constructors Overloading in Java<\/h2>\n\n\n\n<p>Similar to\u00a0Java method overloading, we can also create two or more constructors with different parameters. This is called constructors overloading.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example 6: Java Constructor Overloading<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class Main {\n\n  String language;\n\n  \/\/ constructor with no parameter\n  Main() {\n    this.language = \"Java\";\n  }\n\n  \/\/ constructor with a single parameter\n  Main(String language) {\n    this.language = language;\n  }\n\n  public void getName() {\n    System.out.println(\"Programming Langauage: \" + this.language);\n  }\n\n  public static void main(String&#91;] args) {\n\n    \/\/ call constructor with no parameter\n    Main obj1 = new Main();\n\n    \/\/ call constructor with a single parameter\n    Main obj2 = new Main(\"Python\");\n\n    obj1.getName();\n    obj2.getName();\n  }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>Programming Language: Java\nProgramming Language: Python<\/samp><\/code><\/pre>\n\n\n\n<p>In the above example, we have two constructors:&nbsp;<code>Main()<\/code>&nbsp;and&nbsp;<code>Main(String language)<\/code>. Here, both the constructor initialize the value of the variable language with different values.<\/p>\n\n\n\n<p>Based on the parameter passed during object creation, different constructors are called and different values are assigned.<\/p>\n\n\n\n<p>It is also possible to call one constructor from another constructor.&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we will learn about Java constructors, their types, and how to use them with the help of examples. What is a Constructor? A constructor in Java is similar to a method that is invoked when an object of the class is created. Unlike\u00a0Java methods, a constructor has the same name as that [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[484],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1187"}],"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=1187"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1187\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=1187"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=1187"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=1187"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}