Constructor in JAVA

In this article, we will go through Java constructors, their types with the help of examples.

What is a Constructor?

Constructor is a special member function in JAVA whose name is the same as the class name. It is used to initialize objects at the time of their creation.

Example 1: Java constructor

class DemoExample {
    DemoExample() {
        // constructor body
    }
}

Enter fullscreen mode Exit fullscreen mode

Here, DemoExample() is a constructor. It has the same name as that of the class and does not have any return type, not even void.

Constructor in JAVA

It has no return type, not even void.

It is invoked at the time of object creation or when an instance is created.


Types of Constructor

  • No-Arguments Constructor
  • Parameterized Constructor
  • Default Constructor

No-Arguments Constructor

  • The no-argument constructor is a constructor that does not accept any parameters/arguments.

Example 2: Java No-argument constructor

class Rectangle {
    double length, width;

    //No-argument constructor
    Rectangle() {
        setDetails(52, 47);
    }

    void setDetails(double l, double w) {
        length = l;
        width = w;
    }

    void display() {
        System.out.println("-----------------------");
        System.out.println("Length: " + length);
        System.out.println("Width: " + width);
        System.out.println("Area: " + calcArea());
    }

    double calcArea() {
        return length * width;
    }
}

class ConstructorDemo1 {
    public static void main(String[] args) {
        Rectangle r1 = new Rectangle();
        r1.display();
    }
}

Enter fullscreen mode Exit fullscreen mode

Example 2 Output

----------------------------
Length: 52.0
Width: 47.0
Area: 2444.0

Enter fullscreen mode Exit fullscreen mode


Parameterized Constructor

  • The Java parameterized constructor can also accept one or more parameters/arguments.
  • Such constructors are known as parameterized constructors or constructors with parameters.

Example 3: Java Parameterized Constructor

class Rectangle {
    double length, width;

    //Parameterized constructor
    Rectangle(double l, double w) {
        setDetails(l, w);
    }

    void setDetails(double l, double w) {
        length = l;
        width = w;
    }

    void display() {
        System.out.println("-----------------------");
        System.out.println("Length: " + length);
        System.out.println("Width: " + width);
        System.out.println("Area: " + calcArea());
    }

    double calcArea() {
        return length * width;
    }
}

class ConstructorDemo2 {
    public static void main(String[] args) {
        Rectangle r1 = new Rectangle(101, 70);
        r1.display();
    }
}

Enter fullscreen mode Exit fullscreen mode

Example 3 Output

----------------------------
Length: 101.0
Width: 70.0
Area: 7070.0

Enter fullscreen mode Exit fullscreen mode


Default Constructor

  • The Java compiler will create a no-argument constructor automatically when the user-defined constructor is not explicitly declared. Such constructor is known as the default constructor.
  • Each class has its own default constructor.
  • The Default constructor do not accept arguments.
  • Constructor declared by compiler initializes the object with default values.
  • Once the user defines its own default constructor, the compiler will use it to initialize an object.

Example 4: Java Default Constructor

class Rectangle {
    double length, width;

    void display() {
        System.out.println("-----------------------");
        System.out.println("Length: " + length);
        System.out.println("Width: " + width);
    }
}

class ConstructorDemo3 {
    public static void main(String[] args) {
        Rectangle r1 = new Rectangle();
        r1.display();
    }
}

Enter fullscreen mode Exit fullscreen mode

Example 4 Output

----------------------------
Length: 0.0
Width: 0.0

Enter fullscreen mode Exit fullscreen mode

Any uninitialized instance variables will be initialized with default values by the default constructor.

Type Default Value
boolean false
byte 0
short 0
int 0
long 0L
char \u0000
float 0.0f
double 0.0d
object Reference null

Read the complete post on our site Constructor in JAVA

Read others posts on our site MeshWorld

Hope you like this!

Keep helping and happy coding

原文链接:Constructor in JAVA

© 版权声明
THE END
喜欢就支持一下吧
点赞11 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容