Constructors in Java
What are Constructors in Java?
Java allows the object to initialize itself when it is
created. This automatic initialization is known as
Constructors. The constructor is a block of code
that initializes the newly created object.A
constructor initializes an object immediately upon
creation. It has the same name as the class in
which it resides and is syntactically similar to a
method. Once defined, the constructor is called
automatically immediately after the object is
created, before the new operator completes it.
Constructors have no return type, not even void.
Defining a Constructor in Java:
This is a simple Java Constructor Declaration
example:
How does Constructor work in Java?
Let’s say we have a class MyClass in the above
declaration. When we create the object of MyClass
like this:
Myclass myclassobj = new Myclass();
The new keyword here creates the object of class
MyClass and invokes the constructor to initialize
this newly created object. Each time an object is
created using the new keyword at least one
constructor (it could be the default constructor) is
invoked to assign initial values to the data
members of the same class.
Why do we need a Constructor?
Constructors initialize the new object, that is, they
set the startup property values for the object. They
might also do other things necessary to make the
object usable. You can distinguish constructors
from other methods of a class because
constructors always have the same name as the
class.
Rules for Constructors:
● The name of the constructor must be the
same as that of the class name in which it
resides.
● Constructors must not have a return type. If
you keep the return type for the
constructor, it will be treated as a method.
● Every class should have at least one
constructor. If you don’t write a constructor
for your class, the compiler will give a
default constructor.
● A constructor in Java cannot be abstract,
final, static, and Synchronized.
● Access modifiers can be used in
constructor declaration to control its access
i.e which other class can call the
constructor.
Types of Constructors in Java:
Basically, there are three types of constructors in
java:
1. Parameterized Constructors
2. Default Constructors
3. Copy Constructors
Parameterized Constructor in Java:
Constructors with parameters that can be used to
initialize the internal state (fields) of the newly
created object are known as Parameterized
Constructors. If we want to initialize fields of the
class with our own values, then use a
parameterized constructor.
Example to Understand Parameterized
Constructor in Java:
package Demo;
import java.io.*;
class Student
// data members of the class.
String name;
int id;
// constructor would initialize
data members
// with the values of passed
arguments while
// object of that class created.
Student (String name, int id)
this.name = name;
this.id = id;
public class
ParameterizedConstructor
public static void main (String
args[])
{
//This would invoke the
parameterized constructor.
Student student1 = new
Student ("Ashok", 101);
System.out.println ("Student
Name: " + student1.name +" and
Student Id: " + student1.id);
Output: Student Name: Ashok and Student Id:
101
Default Constructor in Java:
A constructor that has no parameter is known as
the default constructor. If we don’t define a
constructor in a class, then the compiler creates a
default constructor(with no arguments) for the
class. Therefore, it is also known as a no-args
constructor. Once the class is compiled it will
always at least have a no-argument constructor. If
you do define a constructor for your class, then the
Java compiler will not insert the default no-
argument constructor into your class.
Sample Program for Default Constructor:
package Demo;
public class DefaultConstructor
public DefaultConstructor ()
System.out.println ("This is
a no-argument constructor");
}
public static void main (String
args[])
new DefaultConstructor();
Output: This is a no-argument constructor
Copy Constructor in Java:
A copy constructor is used for copying the values
of one object to another object.
Example to Understand Copy Constructor in
Java:
package Demo;
public class CopyConstructor
String web;
CopyConstructor (String w)
web = w;
/* This is the Copy Constructor,
it
* copies the values of one
object
* to the another object (the
object
* that invokes this
constructor)
*/
CopyConstructor (CopyConstructor
cc)
web = cc.web;
void disp ()
System.out.println
("Constructor: " + web);
}
public static void main (String
args[])
CopyConstructor obj1 =new
CopyConstructor ("Example of Copy
Constructor in Java");
/* Passing the object as an
argument to the constructor
* This will invoke the
copy constructor
*/
CopyConstructor obj2 = new
CopyConstructor (obj1);
obj1.disp ();
obj2.disp ();
Output:
Example of Copy Constructor in Java
Example of Copy Constructor in Java
Constructor Overloading in Java
More than one constructor with a different
signature in a class is called constructor
overloading. The signature of the constructor
includes the number, type, and sequence of
arguments.
If two constructors in the same class have the
same signature, it represents ambiguity. In this
case, the Java compiler will generate an error
message because the compiler will unable to
differentiate which form to use.
Hence, an overloaded constructor must have
different signatures. The Java compiler decides
which constructor has to be called depending on
the number of arguments passing with the object.
Example to Understand Constructor
Overloading in Java:
package Demo;
class Student1
int Roll;
String Name;
double Marks;
Student1(int R,String N,double M)
// Constructor 1
Roll = R;
Name = N;
Marks = M;
Student1(String N,double M,int R)
// Constructor 2
Roll = R;
Name = N;
Marks = M;
}
void Display()
System.out.print("\n\t" +
Roll+"\t" + Name+"\t" + Marks);
class ConstructorOverloading
public static void main(String[]
args)
{
Student1 S1 = new
Student1(1,"Kumar",78.53); //
Statement 2
Student1 S2 = new
Student1("Sumit",89.42,2); //
Statement 1
System.out.print("\n\tRoll\
tName\tMarks\n");
S1.Display();
S2.Display();
Output: