Default Constructor in Java

In this article, we will discuss constructors in java, the default constructors in Java, the purpose of default constructors in Java, and the difference between constructors and methods in Java.

What is a Constructor in Java?

A constructor in Java is a section of code that is identical to the method. When a class instance is created, it is called. Memory for the object is allocated in the memory at the time constructor is called.

Constructor is a special type of method that is used to initialize an instance of a class in java.

Every time a constructor is created using new keyword, then at least one constructor is called.

If there is no constructor available in the class then the default constructor is called. By default, the Java compiler provides a default constructor.

Point to Remember: Because it constructs the values when an object is created, it is termed a constructor. The creation of a constructor for a class is not necessary. It’s because if your class doesn’t have a constructor, the Java compiler produces one.

Conditions for Creating Constructors in Java

There are three conditions defined for the constructor in Java:-

  1. The name of the constructor must be the same as its class name.
  2. A Constructor must have no return type.
  3. A Java constructor cannot be –
    • Static
    • Abstract
    • Final
    • Synchronized

Types of Constructors in Java

  1. No-arg constructor.
  2. Parameterized constructor.
  3. Default constructor.

No-arg Constructor in Java

The No-arg constructor is a constructor that accepts no arguments. If we don’t define a constructor in a class, the compiler automatically builds one for the class (with no arguments). Additionally, the compiler does not produce a default constructor if we write a constructor with arguments or without arguments.

Parameterized Constructor in Java

Parameterized constructors are constructors that take parameters. Use a parameterized constructor if you wish to provide your own values as the default values for the class’s fields.

Default Constructor in Java

When the program is executed, the Java compiler automatically constructs a no-arg constructor if we don’t create one ourselves. The default constructor is the name given to this constructor.

Here is an Example:

public class Person {
  String firstName;
  String lastName;
  int age;
}
public static void main(String args[]) {
    Person person1= new Person();

    person1.firstName = "Manoj";
    person1.lastName = "Kumar";
    person1.age = 21;
    System.out.println(person1.age);   
    System.out.println(person1.firstName);
}

Output:

21
Manoj

Did you notice? we did not define any constructor before creating person1 to initialize the attributes created in the class.
This will not throw an error. Rather, the compiler will create an empty constructor but we will not see this constructor anywhere in the code – this happens behind the scenes.
When the compiler begins to run the code, it will seem as follows:

public class Person {
  String firstName;
  String lastName;
  int age;

  // default constructor
  Person() { }

}
public static void main(String args[]) {
    Person person1= new Person();

     person1.firstName = "Manoj";
     person1.lastName = "Kumar";
     person1.age = 21;

     System.out.println(person1.age);   
     System.out.println(person1.firstName);
}

Many people mistakenly confuse the default constructor with the no-arg constructor, however in Java, they are two different constructors. In Java, a constructor that was developed specifically by the programmer is not regarded as a default constructor.

Purpose of Default Constructor in Java

In Java, a default constructor is a constructor that is automatically generated by the compiler if no other constructors are defined by a programmer in a class. Its purpose is to initialize the object’s attributes to their default values.

Java has a default constructor, which takes no arguments and has an empty body. The default constructor is automatically created by the compiler if no constructors have been defined by the user in the class.

For example, if a class has an attribute x with a default value of 0, the default constructor would initialize the value of x to 0 for any new object created from that class.

It’s also possible to define a constructor with parameters in the class, this constructor will be called when an object is created with the provided parameters.

In summary, the purpose of a default constructor in Java is to provide a basic initialization of an object’s attributes when it is created, so that the object can be used without having to manually set the attribute values.

The Key Differences between Constructors and Methods in Java

In Java, a constructor is a special type of method that is used to initialize an object when it is created. Constructors have the same name as the class, and they do not have a return type. They are automatically called when an object is created using the "new" keyword.

A method, on the other hand, is a block of code that performs a specific task. Methods have a return type, and they can be called explicitly by the programmer. Methods can be used to perform operations on the object’s data, and they can also be used to return a result or a value.

In short:

  • Constructors are used to initialize an instance when it is created.
  • Methods are used to perform operations on an object’s data and can return a result or value.
  • Constructors have the same name as the class and do not have a return type.
  • Methods have a return type and can be called explicitly by the programmer.

Conclusion
In this article, we discussed what are constructors in Java, and what are conditions to follow in order to create a constructor in java, we also look at the types of constructors in java. We also talked about default constructors in java and how constructors are different from methods in java.
Keep Coding!

Leave a Reply

Your email address will not be published. Required fields are marked *