Java Constructor | Default | Class | Types with Examples

A Java Constructor is a method, it’s invoked when a new object of the class has created and memory allocated to the Object. It has the same name as a class name and looks similar to a method but without return any value (no return type). This is a java constructor definition.

The main reason to use constructor is the initial values instance variables of the class. Or You can perform any other start-up procedures required to create a fully formed object.

Java Constructor Default Class Types with Examples

This tutorial you will learn about Java Class Constructor Overview and type with examples.

Java constructor types

Here are 3 types of Constructors in Java:

  • Default constructor – If you do not implement any constructor in your class, the Java compiler inserts a default constructor into your code on your behalf.
  • No argument Constructors – Constructor without arguments, Signature, and code is the same as Default Constructor.
  • Parameterized Constructors – Constructor with arguments or parameters, used for Initialization instance variables.

Syntax

Here is java constructor syntax in java, if you didn’t write any, then by the compiler automatically create it.

class ClassName {
    //constructor
    ClassName() {
    }
}

Java constructor example

Here we are covering the example of Default, no Argument, and Parameterized Constructors.

Default constructor in java

You can define by yourself as above syntax or it will look like the below code. If there are no constructors then the Java compiler will generate one by default.

Default constructor in java example
class Student {
    Student(){}
}

No Argument Constructor Example 

A Without Argument used constructor but with some statement in the body, called No Argument Constructor. Here is an example code of it.

class ClassName {
    public ClassName() {
        System.out.println("This Example of No Argument Constructor");
    }

    public static void main(String args[]) {

        //Creating new object
        ClassName cn = new ClassName();
    }
}

Output: This Example of No Argument Constructor

Parameterized Constructor Example 

With is you can pass the initial value for Instance variables.

Note: If using the same name of the variable, then use this keyword. For example, using id and name the same variable name of the class variable, so it must be you use this keyword with instance variables.

class Student{
    int id;
    String name;

    Student(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public static void main(String args[]) {
        Student obj1 = new Student(9245, "John");
        Student obj2 = new Student(9232, "Tom");
    }
}

Copy constructor in java

A copy constructor is used for copying the values of one object to another object. Here is a simple example of it.

class Student {
    String msg;

    //Parameterized constructor
    Student(String value) {
        this.msg = value;
    }

    //copy constructor
    Student(Student stu){
        msg = stu.msg;

    }

    void disp() {
        System.out.println("Message : " + msg);
    }

    public static void main(String args[]) {
        Student obj1 = new Student("Hello copy");
        Student obj2 = new Student(obj1);

        obj1.disp();
        obj2.disp();
    }
}

Output: Message: Hello copy
Message: Hello copy

Guideline of creating a constructor

  1. Constructor name must be the same as it’s class name
  2. A Constructor must have no explicit return type
  3. Can’t be static, final, abstract and synchronized in Java

QA: Why we use the constructor in Java and when it called?

Constructors in Java is a method which is used to set initial values for field variables. In Java when the object is created, the compiler calls or Create the constructor first.

More: Constructor overloading in java we will cover in the Overloading tutorial.

Do comment if you have any doubt and suggestion on this tutorial.

Note: This example (Project) is developed in IntelliJ IDEA 2018.2.6 (Community Edition)
JRE: 11.0.1
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.14.1

Java version 11

All Java Examples are in Java 11, so it may change its different from Java 9 or 10 or upgraded versions.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.