

{"id":12260,"date":"2018-03-30T08:56:27","date_gmt":"2018-03-30T08:56:27","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=12260"},"modified":"2026-05-16T18:26:10","modified_gmt":"2026-05-16T12:56:10","slug":"constructor-in-java","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/constructor-in-java\/","title":{"rendered":"Constructor in Java with Example"},"content":{"rendered":"<p>Constructors\u2026 The name itself is so creative in nature, isn\u2019t it?<\/p>\n<p>Well, constructors are basically the mould for objects. It gives the object its shape and meaning.<\/p>\n<p><strong>Example time!<\/strong><\/p>\n<p>If you ever visit a factory where some hot metal is being pushed through a mould, you will see exactly why I compared constructors with moulds in the first place.<\/p>\n<p>The metal, molten, fuming hot, has a very unique property: it is a liquid. It can change its shape to any possible shape you can imagine. The mould gives it the shape and identity.<\/p>\n<p>Constructors in Java do the same.<\/p>\n<h3>What is a constructor in Java?<\/h3>\n<p>A constructor in Java is simply a bundle of statements that are particularly useful for initialising the object with default values.<\/p>\n<p>For example, when you are declaring an object, you may want the class variables to start with some default value, right? Well, constructors are the ideal tool for that.<\/p>\n<p>You can also use the constructors to allow class variables to take up specific user-defined values during object definition.<\/p>\n<p>This simply means that the user can specify explicit values to the class members during each object creation. These are parameterised constructors. We will be reading about them shortly.<\/p>\n<p>A constructor always has the same name as that of a class. When you create a new object of a class, the compiler calls the constructor of the class.<\/p>\n<p>If no constructor is present in the class explicitly, then the compiler declares a default compiler for itself and initialises the class variables automatically. This constructor is the default constructor in Java.<\/p>\n<p>A constructor is useful for initialising the properties of the objects. As soon as the new() method gets executed the constructor for the class gets called.<\/p>\n<p><strong>Syntax of Java constructor:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">class &lt;class_name&gt;\r\n{\r\nint class_var1,class_var2;\r\n\r\n\/\/the constructor \r\n\r\n&lt;class_name&gt;()\r\n{\r\n    \/\/this is the constructor \r\n    \/\/This is where you initialize the class variables. \r\n    class_var1=0;\r\n    class_var2=2;\r\n}\r\n}\r\n<\/pre>\n<p><em><strong>Note that even if we do not explicitly mention a constructor, the compiler will add a constructor by default and assign all variables with their default values.<\/strong><\/em><\/p>\n<p>These are the basics of constructors in Java.<\/p>\n<p>There are certain rules that we should keep in mind when writing constructors.<\/p>\n<ul>\n<li>The constructor should have the same name as the class.<\/li>\n<li>A constructor can not be static, final or synchronised.<\/li>\n<li>You can use access modifiers to limit access to the constructor.<\/li>\n<\/ul>\n<h3>Need for Constructors in Java<\/h3>\n<p>Constructors are generally useful for writing user-specific values to the instance variables.<\/p>\n<p>These can also be used when the programmer needs to set explicit or default values to the member variables of the class.<\/p>\n<h3>Types of Constructors in Java<\/h3>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/03\/Constructors-in-Java.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-84875\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/03\/Constructors-in-Java.jpg\" alt=\"Constructors in Java\" width=\"1200\" height=\"628\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/03\/Constructors-in-Java.jpg 1200w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/03\/Constructors-in-Java-300x157.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/03\/Constructors-in-Java-1024x536.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/03\/Constructors-in-Java-150x79.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/03\/Constructors-in-Java-768x402.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/03\/Constructors-in-Java-720x377.jpg 720w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/03\/Constructors-in-Java-520x272.jpg 520w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/03\/Constructors-in-Java-320x167.jpg 320w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><\/a><\/p>\n<p>There are a few types of constructors in Java. Let us get to know them one by one.<\/p>\n<h4>Java Default Constructor<\/h4>\n<p>The default constructor in Java has no parameters. When a programmer does not specify a constructor in the code, then the compiler adds it implicitly.<\/p>\n<p>It assigns default values to objects and the variables based on the datatype. Well enough talk. Let us get our hands dirty!<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">package com.dataflair.constructorsinjava\r\npublic class ConstructorTypes\r\n{\r\n    int defaultvalueint;\r\n    String defaultString;\r\n    public static void main(String[] args) {\r\n        System.out.println(\"This is the default constructor at work. \");\r\n\r\n        ConstructorTypes object = new ConstructorTypes();\/\/We called the default constructor here\r\n        System.out.println(\"The default values of int data type is \"+object.defaultvalueint);\r\n        System.out.println(\"The default values of the String data type is \"+object.defaultString);\r\n\r\n        \r\n    }\r\n}\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">This is the default constructor at work.<br \/>\nThe default values of int data type is 0<br \/>\nThe default values of the String data type is null<\/div>\n<h4>Java No Parameter Constructor<\/h4>\n<p>A no-parameter constructor is similar to what the compiler creates when we do not explicitly declare a constructor. This is also a default constructor. This type of constructor does not take any parameters.<\/p>\n<p>Let us see an example of a Java Constructor with no parameters:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">package com.dataflair.constructorsinjava\r\npublic class ConstructorTypes\r\n{\r\n    int defaultvalueint;\r\n    String defaultString;\r\n    ConstructorTypes()\r\n    {\r\n        System.out.println(\"This is the default constructor... It doesn\u2019t do much except assigning default values to class variables.\");\r\n    }\r\n    public static void main(String[] args) {\r\n\r\n        ConstructorTypes object = new ConstructorTypes();\/\/We called the default constructor here\r\n        System.out.println(\"The default values of int data type is \"+object.defaultvalueint);\r\n        System.out.println(\"The default values of the String data type is \"+object.defaultString);\r\n\r\n\r\n    }\r\n}\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">This is the default constructor&#8230; It doesn\u2019t do much except assigning default values to class variables.<br \/>\nThe default values of int data type is 0<br \/>\nThe default values of the String data type is null<\/div>\n<h4>Java Parameterised Constructor<\/h4>\n<p>The real power of constructors can be leveraged by using parameterised constructors.<\/p>\n<p>A parameterised constructor allows passing a list of parameters in the constructor definition. Therefore, it gives an advantage to set the starting values of the object. Just like a method can have parameters of different data types, in the same way, a constructor can also have a parameter list of multiple data types.<\/p>\n<p>You can explicitly set the values of the class variables while declaring objects with the help of parameterised constructors.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">package com.dataflair.constructorsinjava\r\npublic class ConstructorTypes\r\n{\r\n    int defaultvalueint;\r\n    String defaultString;\r\n    ConstructorTypes(int val, String value)\r\n    {\r\n        System.out.println(\"This is the parameterized constructor\");\r\n        defaultString=value;\r\n        defaultvalueint=val;\r\n    }\r\n    public static void main(String[] args) {\r\n\r\n        ConstructorTypes object = new ConstructorTypes(52,\"DataFlair\");\/\/We called the default constructor here\r\n        System.out.println(\"The default values of int data type is \"+object.defaultvalueint);\r\n        System.out.println(\"The default values of the String data type is \"+object.defaultString);\r\n\r\n\r\n    }\r\n}\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">This is the parameterized constructor<br \/>\nThe default values of int data type is 52<br \/>\nThe default values of the String data type is DataFlair<\/div>\n<p>A point to note would be that constructors cannot return values. However, you can return the instance of the current class with the help of constructors.<\/p>\n<h3>Constructor Chaining in Java<\/h3>\n<p>As the name suggests, the chaining of constructors is simply calling a different constructor each time when the control shifts inside a particular constructor.<\/p>\n<p>Simply, it is the linking of multiple constructors in a class.<\/p>\n<p>You can achieve constructor chaining with the help of \u201cthis\u201d keyword.<\/p>\n<p>But why is this concept in practice? I mean, wouldn\u2019t it be easy to declare the constructors and call them individually anyway?<\/p>\n<p>Well yes, but constructor chaining particularly proves to be helpful when you need to do multiple tasks in a single constructor.<\/p>\n<p>The consecutive calls to the constructors help in defining each and every value.<\/p>\n<p>One important point to note is that the this() call should be before any other statement in the constructor. If not then the compiler returns an error.<\/p>\n<p>It is time for another example again!<\/p>\n<p><strong>Java program to illustrate Java constructor chaining:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">package com.dataflair.constructorsinjava;\r\npublic class ConstructorChaining {\r\n\r\n    ConstructorChaining()\r\n    {\r\n        this(100);\r\n        \/\/call made to constructor with one parameter of type int\r\n        System.out.println(\"This is the default constructor \");\r\n\r\n    }\r\n    ConstructorChaining(int x)\r\n    {\r\n        this(x,\"DataFlair\");\r\n        \/\/constructor call made with two parameters. \r\n        System.out.println(\"This is a parameterized constructor\");\r\n\r\n\r\n    }\r\n    ConstructorChaining(int x, String s)\r\n    {\r\n        System.out.println(s+\" \"+x);\r\n        System.out.println(\"This is the final constructor call. \");\r\n    }\r\n        public static void main(String[] args) {\r\n\r\n            ConstructorChaining object = new ConstructorChaining();\r\n            \r\n            \r\n        }\r\n}\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">DataFlair 100<br \/>\nThis is the final constructor call.<br \/>\nThis is a parameterized constructor<br \/>\nThis is the default constructor<\/div>\n<h3>Super Method in Java<\/h3>\n<p>When the super method gets executed, the control shifts to the constructor of the parent class.<\/p>\n<p>During the initialisation of a child class object, the compiler implicitly calls the superclass constructor.<\/p>\n<p>Let us see with an example<\/p>\n<p><strong>Java program to illustrate the use of the super() method:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">package com.dataflair.constructorsinjava;\r\nclass A\r\n{\r\n    A()\r\n    {\r\n        System.out.println(\"This is the constructor of class A\");\r\n    }\r\n\r\n}\r\nclass B extends A\r\n{\r\n    B()\r\n    {\r\n        super();\r\n        System.out.println(\"This is the constructor of class B\");\r\n    }\r\n}\r\npublic class SuperConstructor {\r\n    public static void main(String[] args) {\r\n        \r\n        B ob = new B();\r\n\r\n    }\r\n}\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">This is the constructor of class A<br \/>\nThis is the constructor of class B<\/div>\n<h3>Java Constructor Overloading<\/h3>\n<p>Similar to function overloading, constructor overloading involves the process of using the same name(which is evident because constructors and the classes share the same name), having different parameters.<\/p>\n<p>The compiler chooses the constructor based on the data types and the number of parameters. This process is similar to function overloading.<\/p>\n<p><strong>Java program to illustrate the use of Constructor Overloading:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">package com.dataflair.constructorsinjava;\r\nclass A\r\n{\r\n    A()\r\n    {\r\n        System.out.println(\"This is the default constructor\");\r\n\r\n    }\r\n    A(int a)\r\n    {\r\n        System.out.println(\"This constructor consists of a single parameter of value \"+a);\r\n    }\r\n    A(int a, String s)\r\n    {\r\n        System.out.println(\"This constructor consists of two parameters of values \"+a+\", \"+s);\r\n    }\r\n}\r\n\r\npublic class ConstructorOverloading {\r\n    public static void main(String[] args) {\r\n     \r\n    A obj=new A();\r\n    A obj1=new A(5,\"DataFlair\");   \r\n    }\r\n    \r\n\r\n}\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">This is the default constructor<br \/>\nThis constructor consists of two parameters of values 5, DataFlair<\/div>\n<h3>Copy Constructor in Java<\/h3>\n<p>Java Copy constructors are particularly useful for copying the values of an object into a different object.<\/p>\n<p>Many a time, some objects need to have the same instance values. This is where copy constructors come in.<\/p>\n<p><strong>Java program to illustrate the use of Copy Constructors:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">package com.dataflair.constructorsinjava;\r\npublic class CopyConstructor {\r\n    String s;\r\n    CopyConstructor(String string)\r\n    {   \r\n        this.s=string;\r\n\r\n    }\r\n    CopyConstructor(CopyConstructor object)\r\n    {\r\n        this.s=object.s;\r\n    }\r\n    public void valuesdisplay()\r\n    {\r\n        System.out.println(\"The object has the values-&gt; \"+s);\r\n    }\r\n    public static void main(String[] args) {\r\n\r\n        CopyConstructor object=new CopyConstructor(\"DataFlair\");\r\n        CopyConstructor copyobject=new CopyConstructor(object);\r\n        object.valuesdisplay();\r\n        copyobject.valuesdisplay();\r\n\r\n        \r\n    }\r\n    \r\n}\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">The object has the values-&gt; DataFlair<br \/>\nThe object has the values-&gt; DataFlair<\/div>\n<p>As you can see, the \u201ccopy object\u201d has the same values of instance variables as the primary object. This is possible due to copy constructors.<\/p>\n<h3>Difference Between Java Constructors and Java Methods<\/h3>\n<table>\n<tbody>\n<tr>\n<td><b>Constructors in Java<\/b><\/td>\n<td><b>Methods in Java<\/b><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left\"><span style=\"font-weight: 400\">Constructors must always have the same name as that of the class.<\/span><\/td>\n<td style=\"text-align: left\"><span style=\"font-weight: 400\">The methods can have any name the programmer wants. However, it should not be an identifier or a reserved word.\u00a0<\/span><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left\"><span style=\"font-weight: 400\">Constructors cannot return a value. It can only return references.\u00a0<\/span><\/td>\n<td style=\"text-align: left\"><span style=\"font-weight: 400\">Methods can return values.\u00a0<\/span><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left\"><span style=\"font-weight: 400\">The JVM invokes constructors implicitly whenever you create a new object.\u00a0<\/span><\/td>\n<td style=\"text-align: left\"><span style=\"font-weight: 400\">You can call methods explicitly.\u00a0<\/span><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left\"><span style=\"font-weight: 400\">If no constructor is present, the JVM implicitly creates a default constructor.\u00a0<\/span><\/td>\n<td style=\"text-align: left\"><span style=\"font-weight: 400\">If a method is not present, the JVM does not create default methods implicitly.\u00a0<\/span><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left\"><span style=\"font-weight: 400\">Constructors are particularly useful for initialising an object.\u00a0<\/span><\/td>\n<td style=\"text-align: left\"><span style=\"font-weight: 400\">Methods are useful when you need to add specific functionality to an object.\u00a0<\/span><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left\"><span style=\"font-weight: 400\">A single constructor is non-inheritable by child classes.\u00a0<\/span><\/td>\n<td style=\"text-align: left\"><span style=\"font-weight: 400\">Methods are inheritable by child classes.\u00a0<\/span><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left\"><span style=\"font-weight: 400\">A single class can have more than one constructor. But no two constructors can have the same parameters.\u00a0<\/span><\/td>\n<td style=\"text-align: left\"><span style=\"font-weight: 400\">A single class can have more than one method. But no two methods can have the same name and the same parameters.\u00a0<\/span><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left\"><span style=\"font-weight: 400\">A constructor cannot be final, static or synchronizable.\u00a0<\/span><\/td>\n<td style=\"text-align: left\"><span style=\"font-weight: 400\">Methods can be static, final or synchronised.\u00a0<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>Points to Remember for Constructors in Java<\/h3>\n<p>1. Firstly, the constructors should have the same name as the class.<\/p>\n<p>2. Constructors cannot return anything except a reference to the object.<\/p>\n<p>3. You can declare a constructor as private, protected, default or public.<\/p>\n<p>4. If there is no explicit mention of a constructor inside a class, the compiler will add a default constructor implicitly.<\/p>\n<p>5. If you are using this() or super(), care should be taken that these statements should be the first lines of code in the constructor scope.<\/p>\n<p>6. You can overload a constructor but you cannot override it.<\/p>\n<p>7. You cannot explicitly inherit a constructor.<\/p>\n<p>8. One cannot instantiate abstract classes. But they do have constructors. These constructors will be invoked when the class which implements the abstract class gets instantiated.<\/p>\n<p>9. The compiler would not insert a default constructor in the child class if the superclass doesn\u2019t have a default constructor.<br \/>\nthis() allows you to chain constructors together.<\/p>\n<p>10. The main difference between methods and constructors is that methods can return values and have a different name. But constructors have the same name as the class itself and can only return references. They cannot return values.<\/p>\n<p>11. Lastly, there are no constructors in interfaces.<\/p>\n<h3>Best Practices for Using the Constructor in Java<\/h3>\n<p>Constructors in Java offer a powerful mechanism for initialising objects and ensuring their state is consistent upon creation. Here are some recommended practices to keep in mind when working with constructors:<\/p>\n<p><strong>1. Favour Parameterised Constructors:<\/strong> While Java provides a default constructor if none is explicitly defined, it&#8217;s generally recommended to create parameterised constructors. This allows you to explicitly set the initial values of object properties during object creation, promoting better code clarity and maintainability.<\/p>\n<p><strong>2. Consider Constructor Chaining:<\/strong> Constructor chaining enables you to call one constructor from another within the same class. This can help initialise objects in a specific order or reuse common initialisation logic across multiple constructors.<\/p>\n<p><strong>3. Adhere to Naming Conventions:<\/strong> Although constructors share the class name, it&#8217;s still a good idea to follow standard naming conventions for Java methods. This promotes code readability and consistency within your project.<\/p>\n<h3>Summary<\/h3>\n<p>In this article, we learned about the various aspects of constructors in Java, its types and how to use them to our advantage.<\/p>\n<p>Constructors may seem like an easy topic but interviewers manage to ask very tricky questions based on it. So it is extremely important to have a concrete knowledge of constructors during interviews.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Constructors\u2026 The name itself is so creative in nature, isn\u2019t it? Well, constructors are basically the mould for objects. It gives the object its shape and meaning. Example time! If you ever visit a&#46;&#46;&#46;<\/p>\n","protected":false},"author":5,"featured_media":84875,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32],"tags":[2932,20823,2937,3709,7443,7466,7634,9097,9404],"class_list":["post-12260","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","tag-constructor-overloading-in-java","tag-constructor-vs-methods","tag-constructors-in-java","tag-default-constructor-in-java","tag-java-constructor-example","tag-java-default-constructor","tag-java-parameterized-constructor","tag-no-argument-constructor-in-java","tag-parameterized-constructor-in-java"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Constructor in Java with Example - DataFlair<\/title>\n<meta name=\"description\" content=\"Constructor in java is a block of code which creates an object. Learn Constructors Vs methods. Explore Constructor overloading &amp; chaining.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/constructor-in-java\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Constructor in Java with Example - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Constructor in java is a block of code which creates an object. Learn Constructors Vs methods. Explore Constructor overloading &amp; chaining.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/constructor-in-java\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2018-03-30T08:56:27+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-05-16T12:56:10+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/03\/Constructors-in-Java.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"9 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Constructor in Java with Example - DataFlair","description":"Constructor in java is a block of code which creates an object. Learn Constructors Vs methods. Explore Constructor overloading & chaining.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/constructor-in-java\/","og_locale":"en_US","og_type":"article","og_title":"Constructor in Java with Example - DataFlair","og_description":"Constructor in java is a block of code which creates an object. Learn Constructors Vs methods. Explore Constructor overloading & chaining.","og_url":"https:\/\/data-flair.training\/blogs\/constructor-in-java\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2018-03-30T08:56:27+00:00","article_modified_time":"2026-05-16T12:56:10+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/03\/Constructors-in-Java.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"9 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/constructor-in-java\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/constructor-in-java\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823"},"headline":"Constructor in Java with Example","datePublished":"2018-03-30T08:56:27+00:00","dateModified":"2026-05-16T12:56:10+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/constructor-in-java\/"},"wordCount":1730,"commentCount":3,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/constructor-in-java\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/03\/Constructors-in-Java.jpg","keywords":["Constructor Overloading in Java","constructor vs Methods","Constructors in Java","Default Constructor in Java","Java Constructor example","Java default constructor","Java parameterized constructor","No-Argument Constructor in Java","Parameterized Constructor in Java"],"articleSection":["Java Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/constructor-in-java\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/constructor-in-java\/","url":"https:\/\/data-flair.training\/blogs\/constructor-in-java\/","name":"Constructor in Java with Example - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/constructor-in-java\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/constructor-in-java\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/03\/Constructors-in-Java.jpg","datePublished":"2018-03-30T08:56:27+00:00","dateModified":"2026-05-16T12:56:10+00:00","description":"Constructor in java is a block of code which creates an object. Learn Constructors Vs methods. Explore Constructor overloading & chaining.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/constructor-in-java\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/constructor-in-java\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/constructor-in-java\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/03\/Constructors-in-Java.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/03\/Constructors-in-Java.jpg","width":1200,"height":628,"caption":"Constructors in Java"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/constructor-in-java\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Java Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/java\/"},{"@type":"ListItem","position":3,"name":"Constructor in Java with Example"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team creates expert-level guides on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our goal is to empower learners with easy-to-understand content. Explore our resources for career growth and practical learning.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam1\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/12260","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=12260"}],"version-history":[{"count":16,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/12260\/revisions"}],"predecessor-version":[{"id":148311,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/12260\/revisions\/148311"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/84875"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=12260"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=12260"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=12260"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}