{"id":29528,"date":"2021-04-01T08:11:49","date_gmt":"2021-04-01T02:41:49","guid":{"rendered":"https:\/\/www.mygreatlearning.com\/blog\/constructor-in-java\/"},"modified":"2025-06-30T22:17:47","modified_gmt":"2025-06-30T16:47:47","slug":"constructor-in-java","status":"publish","type":"post","link":"https:\/\/www.mygreatlearning.com\/blog\/constructor-in-java\/","title":{"rendered":"Constructors in Java"},"content":{"rendered":"\n<p>We all know that an object of a particular class contains instance variables, but if we want to perform some operations on the instance variables, we need to initialize them. This is where a constructor comes in. A constructor is defined with the same name as the class. Such a method is called a constructor.<\/p>\n\n\n\n<p>Hence, a constructor is a method having the same name as that of the class and is used to initialize the instance variables of the objects.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"need-of-constructor\">Need of Constructor<\/h2>\n\n\n\n<p>When you make various objects of a class, data members are automatically allocated under each object. If you are allowed to initialize data members at the time of declaration in class, then the data members corresponding to all the objects will possess the same initial values.<\/p>\n\n\n\n<p>But in practice, you would want to have different initial values for the instance variables of the objects, and in case you use any member method to initialize the data members, you may have to call it separately every time after creating an object.<\/p>\n\n\n\n<p>The creators of Java introduced constructors to simplify object initialization. Hence, practically, you require such a member method that can automatically be called while creating an object to initialize its elements. To do so, you need a CONSTRUCTOR. So let us dive deep into the constructors used in Java.<\/p>\n\n\n\n<p>So, constructors are used to allocating values to the class variables during object creation, which is either explicitly done by the developer\/programmer or by a default constructor in Java itself.<\/p>\n\n\n\n    <div class=\"courses-cta-container\">\n        <div class=\"courses-cta-card\">\n            <div class=\"courses-cta-header\">\n                <div class=\"courses-learn-icon\"><\/div>\n                <span class=\"courses-learn-text\">Academy Pro<\/span>\n            <\/div>\n            <p class=\"courses-cta-title\">\n                <a href=\"https:\/\/www.mygreatlearning.com\/academy\/premium\/master-java-programming\" class=\"courses-cta-title-link\">Java Programming Course<\/a>\n            <\/p>\n            <p class=\"courses-cta-description\">Learn Java the right way! Our course teaches you essential programming skills, from coding basics to complex projects, setting you up for success in the tech industry.<\/p>\n            <div class=\"courses-cta-stats\">\n                <div class=\"courses-stat-item\">\n                    <div class=\"courses-stat-icon courses-user-icon\"><\/div>\n                    <span>16.05 Hrs<\/span>\n                <\/div>\n                <div class=\"courses-stat-item\">\n                    <div class=\"courses-stat-icon courses-star-icon\"><\/div>\n                    <span>3 Projects<\/span>\n                <\/div>\n            <\/div>\n            <a href=\"https:\/\/www.mygreatlearning.com\/academy\/premium\/master-java-programming\" class=\"courses-cta-button\">\n                Learn Java Programming\n                <div class=\"courses-arrow-icon\"><\/div>\n            <\/a>\n        <\/div>\n    <\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"syntax-to-define-a-constructor\">SYNTAX TO DEFINE A CONSTRUCTOR:<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n\nclass &amp;lt;class name&gt;{\ndataMember1;\ndataMember2;\ndataMember3;\n...............\ndataMembern;\n&amp;lt;class name&gt;(){\ndataMember1 = value;\ndataMember2 = value;\ndataMember3 = value;\n..................\ndataMembern = value;\n}\n    \n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"example\">Example:<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n\nclass Item{\n    int a;\n    float b;\n    char c;\n    String s;\n\n    Item() { \/\/ Constructor\n        a=0;\n        b=0.0;\n        c = &#039; &#039;; \/\/Initializing instance variables\n        s = &quot;&quot;;\n    }\n}\n    \n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"invoking-a-constructor\">INVOKING A CONSTRUCTOR<\/h2>\n\n\n\n<p>A constructor is invoked at the time of creating an object of the class. The syntax of invoking a constructor is:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n\n&amp;lt;ClassName&gt; objectName = new ClassName();\n    \n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"example\">Example:<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n\nItem ob = new Item();\n    \n<\/pre><\/div>\n\n\n<p>Here,<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>Item<\/code> is the class name.<\/li>\n\n\n\n<li><code>ob<\/code> represents an object.<\/li>\n\n\n\n<li><code>new<\/code> is a keyword or operator.<\/li>\n\n\n\n<li><code>Item()<\/code> is calling the constructor. (In this case, it\u2019s a default constructor)<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"when-is-a-constructor-called\">When is a Constructor called?<\/h2>\n\n\n\n<p>Whenever an object is created using a <code>new()<\/code> keyword, a constructor (could be a default constructor) is invoked to assign initial values to the data members of the same class.<\/p>\n\n\n\n<p>A constructor is invoked during object or instance creation. For Example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n\nclass GreatLearning{\n    \/\/ A Constructor\n    new GreatLearning() {}\n}\n\/\/ We can create an object of the above class\n\/\/ using the below statement. This statement\n\/\/ calls above constructor.\nGreatLearning obj = new GreatLearning();\n    \n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"rules-you-should-have-in-mind-while-using-a-constructor\">Rules you should have in mind while using a constructor:<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Constructors of a class should have the same name as the class name.<\/li>\n\n\n\n<li>A constructor cannot be abstract, final, static, or synchronized.<\/li>\n\n\n\n<li>Access modifiers can be used in constructor declaration to control its access, i.e., which other class can call the constructor.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"features-of-a-constructor\">Features of a constructor:<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The constructor is defined with the same name as that of the class. Concerning the above example, the method <code>Item()<\/code> has the same name as the class name <code>Item<\/code>. Hence, it is a constructor.<\/li>\n\n\n\n<li>The constructor is only used to initialize the data members and instance variables.<\/li>\n\n\n\n<li>The constructor is automatically called while creating an object. When an object is created, the constructor gets called implicitly. You need not call the constructor through the object like other member methods.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example\">Example:<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n\nItem ob = new Item(); \/\/ This calls the constructor Item()\n    \n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\">\n<li>The constructor needs no return type.<\/li>\n<\/ul>\n\n\n\n<p>A constructor is used only to initialize the data members. No arithmetic or logical operation is performed in a constructor. Hence, the return type of the constructor is not at all required.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The constructor can be public, private, as well as protected. A constructor is always called from outside the class while creating an object. Hence, the access specifier of the constructor by default is public, but we can also declare the constructor as private or protected, but we would not be able to create the object of the class. Private constructors find their application in the singleton design patterns.<\/li>\n\n\n\n<li>The constructor is overloaded automatically.<\/li>\n<\/ul>\n\n\n\n<p>Several constructors created for a class are automatically overloaded as they will possess the same name as the class name and will contain different types of parameters.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"types-of-constructor\">Types of constructor<\/h2>\n\n\n\n<p>There are four different types of constructors in Java:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"default-constructor\">Default constructor:<\/h3>\n\n\n\n<p>A constructor used to initialize the instance variables with the default values is called a default constructor. A constructor that contains no parameter is known as the default constructor. The compiler creates a default constructor for the class if we do not do it by ourselves. If we define any constructor (parameterized or not), the compiler does not generate a default constructor.<\/p>\n\n\n\n<p>Whenever no constructor is defined in a program, the compiler creates a constructor of its own. Whenever an object of a particular class is created, it uses this constructor to initialize the instance variables with the default values.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n\n\/\/ Java Program to illustrate calling a\n\/\/ no-argument constructor\nimport java.io.*;\n\nclass GL{\n    char c;\n    int a;\n    long l;\n    float f;\n    double d;\n    String s;\n\n    \/\/ this would be invoked while an object\n    \/\/ of that class is created.\n    void display(){\n        System.out.println(&quot;Initial value of c is&quot; + c);\n        System.out.println(&quot;Initial value of a is&quot; + a);\n        System.out.println(&quot;Initial value of l is&quot; + l);\n        System.out.println(&quot;Initial value of f is&quot; + f);\n        System.out.println(&quot;Initial value of d is&quot; + d);\n        System.out.println(&quot;Initial value of s is&quot; + s);\n    }\n\n    public static void main(String&#x5B;] args){\n        GL ob = new GL(); \/\/ Calling default Constructor\n        ob.display();\n    }\n}\n    \n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"output\">Output:<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n\nInitial value of c is\nInitial value of a is 0\nInitial value of l is 0\nInitial value of f is 0.0\nInitial value of d is 0.0\nInitial value of s is null\n    \n<\/pre><\/div>\n\n\n<p><strong>Note:<\/strong> Initial value for a character variable is ''. It represents a null character. Hence, the initial value of c is not displayed in the BlueJ terminal Window (As shown above).<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n\nclass GL{\n    public static void main (String&#x5B;] args){\n        \/\/ this would invoke the default constructor.\n        GL gL1 = new GL();\n        \/\/ Default constructor provides the default\n        \/\/ values to the object like 0, null\n        System.out.println(gL1.name);\n        System.out.println(gL1.num);\n    }\n}\n    \n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"output\">Output:<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n\nConstructor called\nnull\n0\n    \n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"non-parameterized-constructor\">Non-parameterized constructor:<\/h3>\n\n\n\n<p>A constructor which initializes the instance variables of an object with definite values readily available within it is known as a Non-parameterized constructor. A non-parameterized constructor is defined with the constructor name along with the empty parameter list. (i.e n_pconst())<\/p>\n\n\n\n<p>Objects can be created in two ways:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Created by the compiler<\/li>\n\n\n\n<li>Created by the programmer<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"icreated-by-the-compiler\">(i)Created by the compiler:<\/h4>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n\n\/\/to illustrate non parameterized constructor\nclass n_const{\n    int a,b;\n    \/\/ Non-parameterized constructor\n    n_const() {\n        a=5;\n        b=8;\n    }\n    void display(){\n        System.out.println(&quot;The value of a is &quot; + a);\n        System.out.println(&quot;The value of b is &quot; + b);\n    }\n}\n    \n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"output\">Output:<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n\nThe value of a is 5.\nThe value of b is 8.\n    \n<\/pre><\/div>\n\n\n<h4 class=\"wp-block-heading\" id=\"iiobject-created-by-the-programmers\">(ii)Object created by the programmers:<\/h4>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n\n\/\/to illustrate non parameterized constructor\nclass n_const{\n    int a,b;\n    \/\/ Non-parameterized constructor\n    n_const() {\n        a=5;\n        b=8;\n    }\n    void display(){\n        System.out.println(&quot;The value of a is &quot; + a);\n        System.out.println(&quot;The value of b is &quot; + b);\n    }\n    public static void main(String&#x5B;] args){\n        n_const ob = new n_const();\n        ob.display();\n    }\n}\n    \n<\/pre><\/div>\n\n\n<p>In the program above <code>n_const()<\/code> is the non-parameterized constructor.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"output\">Output:<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n\nThe value of a is 5.\nThe value of b is 8.\n    \n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"parameterized-constructor\">Parameterized Constructor:<\/h3>\n\n\n\n<p>Constructors that take arguments as input are termed parameterized constructors. As soon as an object is declared in a parameterized constructor, the initial values are passed as arguments to the constructor. The normal way may not work. The constructors can be called explicitly and also implicitly. If we call the method explicitly, it is also called a shorthand function.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n\n\/\/ Java Program to illustrate calling of\n\/\/ parameterized constructor.\nimport java.io.*;\n\nclass GL{\n    \/\/ data members of the class.\n    String name;\n    int id;\n\n    \/\/ constructor would initialize data members\n    \/\/ with the values of passed arguments while\n    \/\/ object of that class created.\n    GL(String name, int id)\n    {\n        this.name = name;\n        this.id = id;\n    }\n}\n\nclass GFG{\n    public static void main (String&#x5B;] args){\n        \/\/ this would invoke the parameterized constructor.\n        GL GL1 = new GL(&quot;adam&quot;, 1);\n        System.out.println(&quot;GLName :&quot; + GL1.name +&quot; and GLId :&quot; + GL1.id);\n    }\n}\n    \n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"output\">Output:<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n\nGLName :adam and GLId :1\n    \n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"can-constructors-return-any-value\">Can constructors return any value?<\/h3>\n\n\n\n<p>There are no \u201creturn value\u201d types of statements in the constructor, but the constructor can return the current class instance. We can write \u2018return\u2019 inside a constructor.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"ivcopy-constructors\">(iv)Copy constructors:<\/h3>\n\n\n\n<p>Java supports the concept of a \"copy constructor\", though it does not provide one by default. It has one formal parameter that is the type of the class (the parameter may be a reference to an object), used to create a copy of an existing object of the same class.<\/p>\n\n\n\n<p>Even though both the classes are the same, it counts as a conversion constructor. The copy constructors we use in Java, abbreviated copy ctor or cctor, have nothing to do with class constructors used in .NET using the same abbreviation. Copy constructors are of two types:<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"1direct-entry-copy-constructor\">1)Direct entry copy constructor:<\/h4>\n\n\n\n<p>The initial value of an object is copied by assigning it to another object.<\/p>\n\n\n\n<p>Eg:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n\nclass copy_con{\/\/class using parameterized copy constructors\n    int a,b;\n    copy_con(int x,int y)\n    {\n        a=x ;\n        b=y ;\n    }\n}\n\nclass abc{\n    public static void main(String&#x5B;] args){\n        copy_con ob=new copy_con(5,8);\n        copy_con ob1=ob;\n    }\n}\n    \n<\/pre><\/div>\n\n\n<h4 class=\"wp-block-heading\" id=\"iicopy-constructor-by-passing-object\">(ii)Copy constructor by passing object<\/h4>\n\n\n\n<p>In this system, the object is passed to the constructor. Further, the instance variables of the current object (i.e., the object through which the constructor is called) are initialized by copying the values from the objects passed to the constructor.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n\nclass copycon{\/\/class using parameterized and copy constructor\n    int a,b;\n    copycon(int x,int y)\n    {\n        a=x ;\n        b=y ;\n    }\n    copycon(copycon p) \/\/copy constructor\n    {\n        a=p.a ;\n        b=p.b ;\n    }\n}\n\nclass abc{\n    public static void main(String args&#x5B;]){\n        copycon ob=new copycon(5,8);\n        copycon ob1=new copycon(ob);\n    }\n}\n    \n<\/pre><\/div>\n\n\n<p><code>copycon ob=new copycon(5,8)<\/code> initializes the instance variables a and b of the object ob with 5 and 8 (with parameterized constructor).<\/p>\n\n\n\n<p>When object ob1 is created, the object ob is passed to the constructor <code>copycon(copycon p)<\/code> which in turn transfers the initial values of ob to the variables a and b of the object ob1 respectively.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"super\">Super()<\/h2>\n\n\n\n<p>Whenever a child class constructor gets invoked, it implicitly invokes the constructor of the parent class. The compiler automatically inserts a <code>super();<\/code> statement at the beginning of the child class constructor.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n\nclass MyParentClass {\n    MyParentClass(){\n        System.out.println(&quot;MyParentClass Constructor&quot;);\n    }\n}\n\nclass MyChildClass extends MyParentClass{\n    MyChildClass() {\n        System.out.println(&quot;MyChildClass Constructor&quot;);\n    }\n    public static void main(String args&#x5B;]) {\n        new MyChildClass();\n    }\n}\n    \n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"output\">Output:<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n\nMyParentClass Constructor\nMyChildClass Constructor\n    \n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"constructor-overloading\">Constructor Overloading<\/h2>\n\n\n\n<p>Like methods, we can overload the constructors for creating objects in different ways. The compiler distinguishes constructors based on the number of parameters, types of the parameters, and order of the parameters.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n\n\/\/ Java Program to illustrate constructor overloading\n\/\/ using same task (addition operation ) for different\n\/\/ types of arguments.\nimport java.io.*;\n\nclass GL{\n    \/\/ constructor with one argument\n    GL(String name)\n    {\n        System.out.println(&quot;Constructor with one &quot; +&quot;argument - String : &quot; + name);\n    }\n\n    \/\/ constructor with two arguments\n    GL(String name, int age)\n    {\n        System.out.println(&quot;Constructor with two arguments : &quot; + &quot; String and Integer : &quot; + name + &quot; &quot;+ age);\n    }\n\n    \/\/ Constructor with one argument but with a different\n    \/\/ type than previous...\n    GL(long id)\n    {\n        System.out.println(&quot;Constructor with an argument : &quot; + &quot;Long : &quot; + id);\n    }\n}\n\nclass GFG{\n    public static void main(String&#x5B;] args){\n        \/\/ Creating the objects of class &#039;GL&#039;\n        \/\/ by passing different arguments\n        \/\/ Invoke the constructor with one argument of\n        \/\/ type &#039;String&#039;.\n        GL GL2 = new GL(&quot;Shikhar&quot;);\n\n        \/\/ Invoke the constructor with two arguments\n        GL GL3 = new GL(&quot;Dharmesh&quot;, 26);\n\n        \/\/ Invoke the constructor with one argument of\n        \/\/ type &#039;Long&#039;.\n        GL GL4 = new GL(325614567);\n    }\n}\n    \n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"output\">Output:<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n\nConstructor with one single argument \u2013 String: Shikhar Constructor with two arguments \u2013 String and Integer: Dharmesh 26 Constructor with one argument \u2013 Long: 325614567\n    \n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"how-are-constructors-different-from-methods-in-java\">How are constructors different from methods in Java?<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Constructors must have the same name as the class within which they are defined, while it is not necessary for methods in Java.<\/li>\n\n\n\n<li>Constructors do not have a return type, while methods have a return type or void if they do not return any value.<\/li>\n\n\n\n<li>A constructor is called only once at the time of Object creation, while method(s) can be called any number of times.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>We all know that an object of a particular class contains instance variables, but if we want to perform some operations on the instance variables, we need to initialize them. This is where a constructor comes in. A constructor is defined with the same name as the class. Such a method is called a constructor. [&hellip;]<\/p>\n","protected":false},"author":41,"featured_media":109262,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"set","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[25860],"tags":[36826],"content_type":[],"class_list":["post-29528","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-software","tag-java"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.3 (Yoast SEO v27.3) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Constructors in Java<\/title>\n<meta name=\"description\" content=\"A constructor is a method having the same name as that of the class and is used to initialize the instance variable of the objects\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.mygreatlearning.com\/blog\/constructor-in-java\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Constructors in Java\" \/>\n<meta property=\"og:description\" content=\"A constructor is a method having the same name as that of the class and is used to initialize the instance variable of the objects\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.mygreatlearning.com\/blog\/constructor-in-java\/\" \/>\n<meta property=\"og:site_name\" content=\"Great Learning Blog: Free Resources what Matters to shape your Career!\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/GreatLearningOfficial\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-04-01T02:41:49+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-06-30T16:47:47+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/constructors-in-java.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"1192\" \/>\n\t<meta property=\"og:image:height\" content=\"630\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"Great Learning Editorial Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/Great_Learning\" \/>\n<meta name=\"twitter:site\" content=\"@Great_Learning\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Great Learning Editorial Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/constructor-in-java\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/constructor-in-java\\\/\"},\"author\":{\"name\":\"Great Learning Editorial Team\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/person\\\/6f993d1be4c584a335951e836f2656ad\"},\"headline\":\"Constructors in Java\",\"datePublished\":\"2021-04-01T02:41:49+00:00\",\"dateModified\":\"2025-06-30T16:47:47+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/constructor-in-java\\\/\"},\"wordCount\":1311,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/constructor-in-java\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/04\\\/constructors-in-java.webp\",\"keywords\":[\"java\"],\"articleSection\":[\"IT\\\/Software Development\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/constructor-in-java\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/constructor-in-java\\\/\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/constructor-in-java\\\/\",\"name\":\"Constructors in Java\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/constructor-in-java\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/constructor-in-java\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/04\\\/constructors-in-java.webp\",\"datePublished\":\"2021-04-01T02:41:49+00:00\",\"dateModified\":\"2025-06-30T16:47:47+00:00\",\"description\":\"A constructor is a method having the same name as that of the class and is used to initialize the instance variable of the objects\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/constructor-in-java\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/constructor-in-java\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/constructor-in-java\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/04\\\/constructors-in-java.webp\",\"contentUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/04\\\/constructors-in-java.webp\",\"width\":1192,\"height\":630,\"caption\":\"Constructors in Java\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/constructor-in-java\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Blog\",\"item\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"IT\\\/Software Development\",\"item\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/software\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Constructors in Java\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/\",\"name\":\"Great Learning Blog\",\"description\":\"Learn, Upskill &amp; Career Development Guide and Resources\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#organization\"},\"alternateName\":\"Great Learning\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#organization\",\"name\":\"Great Learning\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/GL-Logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/GL-Logo.jpg\",\"width\":900,\"height\":900,\"caption\":\"Great Learning\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/GreatLearningOfficial\\\/\",\"https:\\\/\\\/x.com\\\/Great_Learning\",\"https:\\\/\\\/www.instagram.com\\\/greatlearningofficial\\\/\",\"https:\\\/\\\/www.linkedin.com\\\/school\\\/great-learning\\\/\",\"https:\\\/\\\/in.pinterest.com\\\/greatlearning12\\\/\",\"https:\\\/\\\/www.youtube.com\\\/user\\\/beaconelearning\\\/\"],\"description\":\"Great Learning is a leading global ed-tech company for professional training and higher education. It offers comprehensive, industry-relevant, hands-on learning programs across various business, technology, and interdisciplinary domains driving the digital economy. These programs are developed and offered in collaboration with the world's foremost academic institutions.\",\"email\":\"info@mygreatlearning.com\",\"legalName\":\"Great Learning Education Services Pvt. Ltd\",\"foundingDate\":\"2013-11-29\",\"numberOfEmployees\":{\"@type\":\"QuantitativeValue\",\"minValue\":\"1001\",\"maxValue\":\"5000\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/person\\\/6f993d1be4c584a335951e836f2656ad\",\"name\":\"Great Learning Editorial Team\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/unnamed.webp\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/unnamed.webp\",\"contentUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/unnamed.webp\",\"caption\":\"Great Learning Editorial Team\"},\"description\":\"The Great Learning Editorial Staff includes a dynamic team of subject matter experts, instructors, and education professionals who combine their deep industry knowledge with innovative teaching methods. Their mission is to provide learners with the skills and insights needed to excel in their careers, whether through upskilling, reskilling, or transitioning into new fields.\",\"sameAs\":[\"https:\\\/\\\/www.mygreatlearning.com\\\/\",\"https:\\\/\\\/in.linkedin.com\\\/school\\\/great-learning\\\/\",\"https:\\\/\\\/x.com\\\/https:\\\/\\\/twitter.com\\\/Great_Learning\",\"https:\\\/\\\/www.youtube.com\\\/channel\\\/UCObs0kLIrDjX2LLSybqNaEA\"],\"award\":[\"Best EdTech Company of the Year 2024\",\"Education Economictimes Outstanding Education\\\/Edtech Solution Provider of the Year 2024\",\"Leading E-learning Platform 2024\"],\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/author\\\/greatlearning\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Constructors in Java","description":"A constructor is a method having the same name as that of the class and is used to initialize the instance variable of the objects","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:\/\/www.mygreatlearning.com\/blog\/constructor-in-java\/","og_locale":"en_US","og_type":"article","og_title":"Constructors in Java","og_description":"A constructor is a method having the same name as that of the class and is used to initialize the instance variable of the objects","og_url":"https:\/\/www.mygreatlearning.com\/blog\/constructor-in-java\/","og_site_name":"Great Learning Blog: Free Resources what Matters to shape your Career!","article_publisher":"https:\/\/www.facebook.com\/GreatLearningOfficial\/","article_published_time":"2021-04-01T02:41:49+00:00","article_modified_time":"2025-06-30T16:47:47+00:00","og_image":[{"width":1192,"height":630,"url":"http:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/constructors-in-java.webp","type":"image\/webp"}],"author":"Great Learning Editorial Team","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/Great_Learning","twitter_site":"@Great_Learning","twitter_misc":{"Written by":"Great Learning Editorial Team","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.mygreatlearning.com\/blog\/constructor-in-java\/#article","isPartOf":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/constructor-in-java\/"},"author":{"name":"Great Learning Editorial Team","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/person\/6f993d1be4c584a335951e836f2656ad"},"headline":"Constructors in Java","datePublished":"2021-04-01T02:41:49+00:00","dateModified":"2025-06-30T16:47:47+00:00","mainEntityOfPage":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/constructor-in-java\/"},"wordCount":1311,"commentCount":0,"publisher":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#organization"},"image":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/constructor-in-java\/#primaryimage"},"thumbnailUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/constructors-in-java.webp","keywords":["java"],"articleSection":["IT\/Software Development"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.mygreatlearning.com\/blog\/constructor-in-java\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.mygreatlearning.com\/blog\/constructor-in-java\/","url":"https:\/\/www.mygreatlearning.com\/blog\/constructor-in-java\/","name":"Constructors in Java","isPartOf":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/constructor-in-java\/#primaryimage"},"image":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/constructor-in-java\/#primaryimage"},"thumbnailUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/constructors-in-java.webp","datePublished":"2021-04-01T02:41:49+00:00","dateModified":"2025-06-30T16:47:47+00:00","description":"A constructor is a method having the same name as that of the class and is used to initialize the instance variable of the objects","breadcrumb":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/constructor-in-java\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.mygreatlearning.com\/blog\/constructor-in-java\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mygreatlearning.com\/blog\/constructor-in-java\/#primaryimage","url":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/constructors-in-java.webp","contentUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/constructors-in-java.webp","width":1192,"height":630,"caption":"Constructors in Java"},{"@type":"BreadcrumbList","@id":"https:\/\/www.mygreatlearning.com\/blog\/constructor-in-java\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog","item":"https:\/\/www.mygreatlearning.com\/blog\/"},{"@type":"ListItem","position":2,"name":"IT\/Software Development","item":"https:\/\/www.mygreatlearning.com\/blog\/software\/"},{"@type":"ListItem","position":3,"name":"Constructors in Java"}]},{"@type":"WebSite","@id":"https:\/\/www.mygreatlearning.com\/blog\/#website","url":"https:\/\/www.mygreatlearning.com\/blog\/","name":"Great Learning Blog","description":"Learn, Upskill &amp; Career Development Guide and Resources","publisher":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#organization"},"alternateName":"Great Learning","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.mygreatlearning.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.mygreatlearning.com\/blog\/#organization","name":"Great Learning","url":"https:\/\/www.mygreatlearning.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/06\/GL-Logo.jpg","contentUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/06\/GL-Logo.jpg","width":900,"height":900,"caption":"Great Learning"},"image":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/GreatLearningOfficial\/","https:\/\/x.com\/Great_Learning","https:\/\/www.instagram.com\/greatlearningofficial\/","https:\/\/www.linkedin.com\/school\/great-learning\/","https:\/\/in.pinterest.com\/greatlearning12\/","https:\/\/www.youtube.com\/user\/beaconelearning\/"],"description":"Great Learning is a leading global ed-tech company for professional training and higher education. It offers comprehensive, industry-relevant, hands-on learning programs across various business, technology, and interdisciplinary domains driving the digital economy. These programs are developed and offered in collaboration with the world's foremost academic institutions.","email":"info@mygreatlearning.com","legalName":"Great Learning Education Services Pvt. Ltd","foundingDate":"2013-11-29","numberOfEmployees":{"@type":"QuantitativeValue","minValue":"1001","maxValue":"5000"}},{"@type":"Person","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/person\/6f993d1be4c584a335951e836f2656ad","name":"Great Learning Editorial Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp","url":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp","contentUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp","caption":"Great Learning Editorial Team"},"description":"The Great Learning Editorial Staff includes a dynamic team of subject matter experts, instructors, and education professionals who combine their deep industry knowledge with innovative teaching methods. Their mission is to provide learners with the skills and insights needed to excel in their careers, whether through upskilling, reskilling, or transitioning into new fields.","sameAs":["https:\/\/www.mygreatlearning.com\/","https:\/\/in.linkedin.com\/school\/great-learning\/","https:\/\/x.com\/https:\/\/twitter.com\/Great_Learning","https:\/\/www.youtube.com\/channel\/UCObs0kLIrDjX2LLSybqNaEA"],"award":["Best EdTech Company of the Year 2024","Education Economictimes Outstanding Education\/Edtech Solution Provider of the Year 2024","Leading E-learning Platform 2024"],"url":"https:\/\/www.mygreatlearning.com\/blog\/author\/greatlearning\/"}]}},"uagb_featured_image_src":{"full":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/constructors-in-java.webp",1192,630,false],"thumbnail":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/constructors-in-java-150x150.webp",150,150,true],"medium":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/constructors-in-java-300x159.webp",300,159,true],"medium_large":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/constructors-in-java-768x406.webp",768,406,true],"large":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/constructors-in-java-1024x541.webp",1024,541,true],"1536x1536":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/constructors-in-java.webp",1192,630,false],"2048x2048":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/constructors-in-java.webp",1192,630,false],"web-stories-poster-portrait":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/constructors-in-java-640x630.webp",640,630,true],"web-stories-publisher-logo":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/constructors-in-java-96x96.webp",96,96,true],"web-stories-thumbnail":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/constructors-in-java-150x79.webp",150,79,true]},"uagb_author_info":{"display_name":"Great Learning Editorial Team","author_link":"https:\/\/www.mygreatlearning.com\/blog\/author\/greatlearning\/"},"uagb_comment_info":0,"uagb_excerpt":"We all know that an object of a particular class contains instance variables, but if we want to perform some operations on the instance variables, we need to initialize them. This is where a constructor comes in. A constructor is defined with the same name as the class. Such a method is called a constructor.&hellip;","_links":{"self":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts\/29528","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/users\/41"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/comments?post=29528"}],"version-history":[{"count":5,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts\/29528\/revisions"}],"predecessor-version":[{"id":109263,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts\/29528\/revisions\/109263"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/media\/109262"}],"wp:attachment":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/media?parent=29528"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/categories?post=29528"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/tags?post=29528"},{"taxonomy":"content_type","embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/content_type?post=29528"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}