Java Constructors
Class has instance variables,methods and constructors also.
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.
when new keyword is used,constructed is invoked.
It can be used to set initial values for object attributes(variables)
Note that the constructor name must match the class name, and it cannot have a return type (like void).
Also note that the constructor is called when the object is created.
All classes have constructors by default: if you do not create a class constructor yourself, Java creates one for you. However, then you are not able to set initial values for object attributes.
Characteristics of Java Constructor
There are the following characteristics or features of constructor in Java. They are as follows:
Constructor’s name must be exactly the same as the class name in which it is defined. It must end with a pair of simple braces.
The constructor should not have any return type even void also because if there is a return type, JVM would consider as a method, not a constructor. Compiler and JVM differentiate constructor and method definitions on the basis of the return type.
Suppose you define the method and constructor with the same name as that of the class name, JVM would differentiate between them by using return type. It is a common mistake to declare the void keyword before a constructor. For example:
public** void** Rectangle() {
}
Here, Rectangle() is a method, not a constructor.
Java constructor may or may not contain parameters. Parameters are local variables to receive value (data) from outside into a constructor.
A constructor is automatically called and executed by JVM at the time of object creation. JVM (Java Virtual Machine) first allocates the memory for variables (objects) and then executes the constructor to initialize instance variables.
It is always called and executed only once per object. This means that when an object of a class is created, constructor is called. When we create second object, the constructor is again called during the second time.
Whenever we create an object/instance of a class, the constructor automatically calls by the JVM . If we don’t define any constructor inside the class, Java compiler automatically creates a default constructor at compile-time and assigns default values for all variables declared in the class.
The default values for variables are as follows:
a. Numeric variables are set to 0.
b. Strings are set to null.
c. Boolean variables are set to false.
In Java, constructors also provide thread safety, meaning no thread can access the object until the execution of constructor is completed.
We can do anything in the constructor similar to a method. Using constructors, we can perform all initialization activities of an object.
Types of Constructors
1.Default Constructor
2.Non-parameterized constructor
3.Parameterized Constructor
Default Constructor
A constructor that has no parameter is known as default constructor in Java. When a class does not declare a constructor, Java compiler automatically adds a constructor for that class.
In other words, the compiler adds a default constructor only when we do not define any constructor explicitly. The constructor added by compiler is called default constructor.
Note:Local variables DO NOT get default values.
Non-parameterized Constructor in Java
A constructor which has no parameters in the parentheses but contains statements inside its body is called non-parametrized constructor. We also call it as a non-argument constructor in Java.
A non-parameterized constructor has the same signature as that of default constructor, except for only one difference between them. Using non-parameterized constructor, we can initialize any values for the instance variables.
A constructor that takes one or more parameters and contains statements inside its body is called parameterized constructor in Java. In the parameterized constructor, instance variables automatically initialize at runtime when we pass values to parameters during object creation.
The parameterized constructor is used to provide different values to distinct objects. It allows us to initialize instance variables with unlike values. In the case of the default constructor, values remain the identical for all objects.
this keyword:
this is a reference variable that refers to the current object.
Why do we need this?
To differentiate between:
Instance variable
Method parameter
Parameterized Constructor:
A constructor that accepts arguments to initialize variables.

Top comments (0)