Class Constructors in Java

Class Constructors in Java

Constructors are an integral part of classes in Java. These are used to initialize an objA constructor is invoked at the time of object or instance creationect’s state. The state variables mainly define the state of an object. Like other methods in a class, a constructor also contains several statements inside it that perform certain functionalities. These statements are executed at Object creation.

Why do we need a Constructor?

To understand the need for a constructor, take the following example of a Box class. If we declare a Box class, the state variables will obviously be the dimensions of the box, which are its length, breadth, and height. However, when translating the operation to creating an object of the Box class, it is not physically possible to have a box with no dimensions. Therefore, there needs to be some method whose sole purpose is to initialize the state of the object. The constructor of the Box class does exactly that. It assigns values to the fields of the class when an object of the class is being created. This is either explicitly done by the programmer or by the default constructor(Java itself).

When is a Constructor called?

During object creation, whenever the new keyword is used to create a new object, at least one constructor is called. This can either be the default constructor or the user-defined constructor. The constructor assigns initial values to the data members of the same class.

A constructor in Java is always invoked at the time of object or instance creation. Consider the following example:

class Demo
{
……
//A constructor
new Demo ()
{
….
}
}
Demo obj = new Demo ();
//The object created by default calls the constructor to initialize its state variables.

 

Rules for Writing Constructor

  • The first and foremost rule of a constructor is that it should have the same name as the class.
  • A constructor in Java can not be
    • static
    • final
    • abstract
    • synchronized
  • Access modifiers like public, protected, and so on can be associated with constructors to restrict the access of other entities into the constructor.

Types of Constructor

There are two types of constructors in Java:

  1. No-argument constructor: No argument constructor is a constructor that has no parameter list and is often referred to as default constructors. If the constructor for a class is not defined, the compiler makes a default constructor of the class that has no arguments. The compiler does not make a default constructor if the class has a user-defined constructor irrespective of the parameter list. Default constructor assigns default values objects like 0, null, etc., depending on the type.
import java.io.*;
class Demo
{
int num;
String name;
Demo ()
{
System.out.println (“Constructor called”);
}
}
class Demo2
{
public static void main (String args [])
{
Demo obj = new Demo();
System.out.println (obj.name);
System.out.println (obj.num);
}
}

 

OUTPUT

Constructor called
null
0

  1. Parameterized Constructor: Parameterized constructors, as the name suggests, have an argument list associated with them in the declaration. This allows users to initialize the fields of the class with any value as required by the program.
    import java.io.*;
    class Demo
    {
    String name;
    int id;
    Demo (String name, int id)
    {
    name = name;
    this.id = id;
    }
    }
    class Demo2
    {
    public static void main (String args [])
    {
    Demo obj = new Demo (“Rahul”, 1);
    System.out.println (“Name :” + obj.name + “ID :” + obj.id);
    }
    }

     

    OUTPUT
    Name: Rahul ID: 1

Do constructors return any value?

Constructors are special methods that do not have any return type, not even void. However, what a constructor really returns is the current class instance. Therefore, this allows us to write ‘return’ inside a constructor.

Leave a comment

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