Thursday, October 28, 2021

Java Constructors

A constructor in Java is a special method that is used to initialize objects. The constructor is called when
an object of a class is created. It can be used to set initial values for object attributes. At the time of calling constructor, memory for the object is allocated in the memory.

Every time an object is created using the new() keyword, at least one constructor is called. It calls a default constructor if there is no constructor available in the class. Java compiler provides a default constructor if there is no constructor available in the class.

Rules for creating a constructor are:

  1. Constructor name must be the same as its class name
  2. A Constructor must have no explicit return type
  3. A Java constructor cannot be abstract, static, final, and synchronized

Types of Java constructors

There are two types of constructors in Java:

  1. Default constructor (no-arg constructor)
  2. Parameterized constructor

1. Default Constructor

A constructor is called "Default Constructor" when it doesn't have any parameter.

A constructor which does not take any parameters is called default constructor. If there is no constructor in a class, compiler automatically creates a default constructor.

The default constructor is used to provide the default values to the object like 0, null, etc., depending on the type.

Syntax of default constructor:

<class_name>()

{

Statements;

}  

Example:

//Java Program to create and call a default constructor  

class Defcons{  

//creating a default constructor  

Defcons(){

System.out.println("default constructor is called");

}  

//main method  

public static void main(String args[]){  

//calling a default constructor  

Defcons b=new Defcons();  

}  

}  

2. Parameterized Constructor

A constructor which has a specific number of parameters is called a parameterized constructor. A constructor which takes parameters is called parameterized constructor.

The parameterized constructor is used to provide different values to distinct objects. 

Example:

//Java Program to demonstrate the use of the parameterized constructor.  

class Student{  

 int id;  

    String name;  

//creating a parameterized constructor  

    Student(int i,String n){  

    id = i;  

    name = n;  

    }  

    //method to display the values  

    void display(){

                System.out.println(id+" "+name);

    }  

    public static void main(String args[]){  

    //creating objects and passing values  

    Student s1 = new Student(111,"Kalyan");  

    Student s2 = new Student(222,"Ashraf");  

    //calling method to display the values of object  

    s1.display();  

    s2.display();  

   }  

}  

Note:

There is no copy constructor in Java. However, we can copy the values from one object to another like copy constructor in C++.

There are many ways to copy the values of one object into another in Java. They are:

  • By constructor
  • By assigning the values of one object into another
  • By clone() method of Object class

Difference between constructor and method in Java

 There are many differences between constructors and methods. They are given below.



Java Constructor



Java Method



A constructor is used to initialize the state of an object.



A method is used to expose the behavior of an object.



A constructor must not have a return type.



A method must have a return type.



The constructor is invoked implicitly.



The method is invoked explicitly.



The Java compiler provides a default constructor if you don't have any
constructor in a class.



The method is not provided by the compiler in any case.



The constructor name must be same as the class name.



The method name may or may not be same as the class name.


0 comments:

Post a Comment

Data Structures with C++



NET/SET/CS PG



Operating Systems



Computer Networks



JAVA



Design and Analysis of Algorithms



Programming in C++

Top