Java Constructors with Examples

In this tutorial, we are going to learn about the constructors in Java, what is constructor in java? How many types of constructors? What is the purpose of constructor? By Preeti Jain Last updated : January 14, 2024

Purpose of Constructor

The purpose of a constructor is to assign initial values to the instance variables at runtime.

Java Constructor

  • A constructor is a special function or method to initialize an object when it is created.
  • Constructor name must be the same as the class name.
  • The syntax of the constructor is similar as method except constructors have no explicit return type.

Types of Java Constructors

There are two types of constructor in java and the name of those constructors given below:

  1. Default Constructor or No argument Constructor
  2. Parameterized Constructor

1. Java Default Constructor or No argument Constructor

  • Default Constructor or No argument constructor is that constructor which takes no argument.
  • In this constructor, we don’t pass any argument or parameter.
  • When we don’t include any constructor in the class so java compiler calls this constructor by default that is the name of this constructor is default constructor.

Syntax

class Classname{
    Classname(){
        // initialization of instance variable
    }
}

Example of Default Constructor or No argument Constructor

import java.util.*;

// Class Declaration
class Constr {
    // Declaring str instance variable of String type
    String str;
    // Constructor Definition
    Constr() {
        str = "Hello World!!";
    }
}

public class Main {
    public static void main(String[] args) {
        // Constructor Calling
        Constr con = new Constr();
        System.out.println(con.str);
    }

}

Output

D:\Programs>javac Main.java
D:\Programs>java Main
Hello World !!

2. Java Parameterized Constructor

  • Parameterized Constructor are those constructor which takes argument explicitly.
  • In this constructor we must pass argument or parameter.
  • When we include both constructor (Default and Parameterized) in the class so java compiler does not call this constructor bydefault (i.e. this constructor will be called explicitly).

Syntax

class Classname{
    Classname(datatype arg1, datatype arg2, ...){
        // initialization of instance variable
    }
}

Example of Parameterized Constructor

import java.util.*;

// Class Declaration
class ParamConstr {
    // Instance Variable
    String str;
    // Constructor Definition
    ParamConstr(String stri) {
        str = stri;
    }
}

public class Main {
    public static void main(String[] args) {
        // Calling Parameterized Constructor
        ParamConstr pcon = new ParamConstr("Hi, Welcome in parametrized constructor");
        System.out.println(pcon.str);
    }
}

Output

D:\Programs>javac Main.java

D:\Programs>java Main
Hi, Welcome in parameterized constructor

3. Java Copy Constructor

A constructor that has one parameter and the parameter is the reference of the same class.

Example of Copy Constructor

class CopyConstructor {
    String name;
    // Constructor defined
    CopyConstructor(String name) {
        this.name = name;
    }
}

class Main {
    public static void main(String[] args) {
        // Calling constructor
        CopyConstructor cc1 = new CopyConstructor("Preeti");

        // Copied cc1 constructor 
        CopyConstructor cc2 = cc1;

        System.out.println("The value of cc1 is :" + cc1.name);
        System.out.println("The value of cc2 is :" + cc2.name);
    }
}

Output

E:\Programs>javac Main.java

E:\Programs>java Main
The value of cc1 is :Preeti
The value of cc2 is :Preeti


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.