<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Ajay pawar</title>
    <description>The latest articles on DEV Community by Ajay pawar (@ajay_pawar_900).</description>
    <link>https://dev.to/ajay_pawar_900</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3503204%2Ff402e31e-c165-42e3-aa38-0885763d0993.jpg</url>
      <title>DEV Community: Ajay pawar</title>
      <link>https://dev.to/ajay_pawar_900</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ajay_pawar_900"/>
    <language>en</language>
    <item>
      <title>Java Constructors Explained: From Basics to Overloading</title>
      <dc:creator>Ajay pawar</dc:creator>
      <pubDate>Tue, 23 Sep 2025 13:53:39 +0000</pubDate>
      <link>https://dev.to/ajay_pawar_900/java-constructors-explained-from-basics-to-overloading-p1i</link>
      <guid>https://dev.to/ajay_pawar_900/java-constructors-explained-from-basics-to-overloading-p1i</guid>
      <description>&lt;p&gt;When you start learning Java, one of the first things you’ll hear about is constructors. Don’t worry if the word sounds a little complicated — it’s actually very simple.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;🔹 What is a Constructor in Java?&lt;/strong&gt;[]&lt;br&gt;
A constructor is a special method in a class that:&lt;br&gt;
1.Has the same name as the class.&lt;br&gt;
2.Does not have a return type (not even void).&lt;br&gt;
3.Runs automatically when you create an object with new.&lt;/p&gt;

&lt;p&gt;👉 In short: Constructors help you initialize (give values to) the object’s variables.&lt;/p&gt;



&lt;p&gt;&lt;strong&gt;🔹 Why Do We Need Constructors?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Imagine you are creating a blueprint for a Car. Each car should have a color and a model.&lt;br&gt;
Without a constructor, you would need to set these values manually every time. But with a constructor, you can initialize values quickly while creating the object.&lt;/p&gt;



&lt;p&gt;&lt;strong&gt;🔹 Types of Constructors&lt;/strong&gt;&lt;br&gt;
1.Default Constructor&lt;br&gt;
2.Parameterized Constructor&lt;br&gt;
3.Overloaded Constructor&lt;br&gt;
4.Copy Constructor&lt;br&gt;
5.No-Argument Constructor&lt;/p&gt;


&lt;h2&gt;
  
  
  1. Default Constructor👉
&lt;/h2&gt;

&lt;p&gt;If you don’t write a constructor, Java gives you one automatically (called the default constructor).&lt;/p&gt;

&lt;p&gt;What is it?&lt;br&gt;
👉&lt;br&gt;
If you don’t write any constructor, Java automatically creates one for you.&lt;br&gt;
It’s invisible in your code but works in the background.&lt;br&gt;
It initializes instance variables to their default values:&lt;/p&gt;

&lt;p&gt;Numbers → 0&lt;br&gt;
Boolean → false&lt;br&gt;
Objects (like String) → null&lt;/p&gt;

&lt;p&gt;here is example for it:&lt;br&gt;
👉&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Car {
    String color;
    String model;
}

public class Main {
    public static void main(String[] args) {
        Car c1 = new Car(); // default constructor is called
        System.out.println(c1.color); // null
        System.out.println(c1.model); // null
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;👉 Here, Java created a hidden empty constructor for us. Since we didn’t set any values, we got the default (null for Strings, 0 for numbers).&lt;/p&gt;

&lt;p&gt;When to use?&lt;br&gt;
Use this when you don’t want to set values while creating objects.&lt;br&gt;
Useful if you plan to set values later using setters or direct assignment.&lt;/p&gt;


&lt;h2&gt;
  
  
  2. Parameterized Constructor
&lt;/h2&gt;

&lt;p&gt;You can create your own constructor and pass values to it.&lt;br&gt;
A constructor you write yourself without parameters.&lt;/p&gt;

&lt;p&gt;here is example for it:👉&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Car {
    String color;
    String model;

    // Constructor with parameters
    Car(String c, String m) {
        color = c;
        model = m;
    }
}

public class Main {
    public static void main(String[] args) {
        Car c1 = new Car("Red", "BMW");
        Car c2 = new Car("Blue", "Tesla");

        System.out.println(c1.color + " " + c1.model); // Red BMW
        System.out.println(c2.color + " " + c2.model); // Blue Tesla
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;👉 Now, we can set values while creating the object. No extra steps needed.&lt;/p&gt;

&lt;p&gt;When to use?&lt;br&gt;
Whenever objects need different values at the time of creation.&lt;br&gt;
Example: When adding products in an e-commerce app, each product has a different price, name, and category.&lt;/p&gt;


&lt;h2&gt;
  
  
  3. Overloaded Constructor
&lt;/h2&gt;

&lt;p&gt;You can create multiple constructors with different parameter lists. This is called constructor overloading.&lt;br&gt;
Having multiple constructors in the same class with different parameter lists.&lt;br&gt;
Java decides which one to call based on the arguments you provide.&lt;/p&gt;

&lt;p&gt;here is example for it:👉&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Car {
    String color;
    String model;

    // No-argument constructor
    Car() {
        color = "White";
        model = "Unknown";
    }

    // One-argument constructor
    Car(String c) {
        color = c;
        model = "Unknown";
    }

    // Two-argument constructor
    Car(String c, String m) {
        color = c;
        model = m;
    }
}

public class Main {
    public static void main(String[] args) {
        Car c1 = new Car();
        Car c2 = new Car("Black");
        Car c3 = new Car("Blue", "Honda");

        System.out.println(c1.color + " " + c1.model); // White Unknown
        System.out.println(c2.color + " " + c2.model); // Black Unknown
        System.out.println(c3.color + " " + c3.model); // Blue Honda
    }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;👉 Same class, but different ways to initialize objects.&lt;br&gt;
When to use?&lt;br&gt;
When you want flexibility in how objects are created.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
Create a Book with just a title.&lt;br&gt;
Or a Book with title + author.&lt;br&gt;
Or a Book with title + author + price.&lt;/p&gt;


&lt;h2&gt;
  
  
  4. Copy Constructor in Java
&lt;/h2&gt;

&lt;p&gt;Unlike C++, Java does not have a built-in copy constructor.&lt;br&gt;
But we can create our own constructor that copies the values of one object into another.&lt;/p&gt;

&lt;p&gt;A copy constructor is a constructor that creates a new object by copying the values from another object of the same class.&lt;/p&gt;

&lt;p&gt;here is example:👉&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Student {
    String name;
    int age;

    // Parameterized constructor
    Student(String n, int a) {
        name = n;
        age = a;
    }

    // Copy constructor
    Student(Student s) {
        name = s.name;
        age = s.age;
    }
}

public class Main {
    public static void main(String[] args) {
        Student s1 = new Student("Ajay", 21);    // original object
        Student s2 = new Student(s1);            // copy object

        System.out.println(s1.name + " - " + s1.age); // Ajay - 21
        System.out.println(s2.name + " - " + s2.age); // Ajay - 21
    }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;✅ Why Use Copy Constructor?&lt;br&gt;
-To duplicate objects easily.&lt;br&gt;
-To create a deep copy (so changes in one object don’t affect the other).&lt;br&gt;
-Useful when working with objects that have many properties.&lt;/p&gt;




&lt;h2&gt;
  
  
  5.No-Argument Constructor
&lt;/h2&gt;

&lt;p&gt;(User-Defined Default Constructor)&lt;br&gt;
What is it?&lt;br&gt;
A constructor you write yourself without parameters.&lt;br&gt;
It lets you set custom default values instead of Java’s built-in defaults.&lt;/p&gt;

&lt;p&gt;When to use?&lt;br&gt;
When you want all new objects to have initial default values.&lt;br&gt;
Example: A new game player might always start with 100 health and 0 score.&lt;/p&gt;




&lt;p&gt;👉 Technically, Java only has 2 main categories:&lt;/p&gt;

&lt;p&gt;1.Default Constructor&lt;br&gt;
2.Parameterized Constructor&lt;br&gt;
The rest are variations made by programmers.&lt;/p&gt;




&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fksms5rtmzv5xkimx1rjq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fksms5rtmzv5xkimx1rjq.png" alt=" " width="800" height="414"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>java</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
