The purpose of the constructor is to initialize the objects after the creation of
objects.
Constructor is not used to create the object.
new keyword is responsible to create the object for the class. It will create an
instance of the class with a copy of the variables whose default values are
assgined. Later constructor comes into the picture to initialize the values of
the variables. To refer to the current object we use this keyword.
this keyword is used to refer the current object name.
Name of the constructor must be same as the name of the class.
example:
class Student
{
String name;
int rollno;
Student(String name, int rollno)
{
[Link] = name;
[Link] = rollno;
}
public static void main(String[] args)
{
Student s1 = new Student("arun", 1201);
Student s2 = new Student("kumar", 1202);
[Link]([Link] + " " + [Link]);
[Link]([Link] + " " + [Link]);
}
}
// The number of times a constructor gets executed is the number of times an
object is created.
class Test
{
Test()
{
[Link]("Constructor");
}
public static void main(String[] args)
{
Test t1 = new Test();
Test t2 = new Test();
Test t3 = new Test();
}
}
Rules for writing the constructor:
1. Name of the constructor and the name of the class must be same.
class Test
{
Test()
{
}
}
2. Return type for the constructor: There is no return type for a constructor
even void. If used by mistake, the compiler will treat it as a method but not
constructor.
class Test
{
void Test()
{
[Link]("Hello");
}
public static void main(String[] args)
{
Test t1 = new Test();
Test t2 = new Test();
[Link]();
}
}
Here the code will be executed and run but there will be no output.
Note: It is legal to have the method name same as the name of the class. But it
is not recommended.
[Link] allowed for constructors:
The only applicable modifiers for constructors are public, default, protected and
private.
Private constructors are helpful to implement singleton classes, where only a
single object will be created.
Default Constructor:
Every class in java including abstract classes contain constructor. If you dont
write any constructor, Compiler generates default constructor, only if you dont
write atleast one constructor.
class Test
{
// Compiler generates default constructor
}
class Test
{
Test(int i)
{
}
// In this case, compiler will not generate any default constructor.
}
There is no chance for both default constructor and user written constructors to
exist.
prototype of Default Constructor [A constructor is generated by compiler]:
1. It is always no - arg constructor.
2. The Acess Modifier of the default constructor is same as class modifier. If
the class is public, Default constructor is also public. Similarly with default
because top class cannot be private or protected.
3. Default constructor contains only one line : super(); It is a no - arg call to
super class constructor.
4. The first line inside the constructor should be either super() or this().
class Test
{
Test()
{
super(); // No - Arg call to super class constructor.
}
}
---------------------------------------------------------------------------------
------
Programmer's Code Compiler's Code
P's Code C's Code
class Test class Test
{ {
Test()
} {
super();
}
}
---------------------------------------------------------------------------------
--------------
public class Test public class Test
{ {
public Test()
{
super();
}
} }
---------------------------------------------------------------------------------
------------------
class Test class Test
{ {
void Test() Test()
{ {
}
super();
} }
void
Test()
{
}
}
---------------------------------------------------------------------------------
----------------------
class Test class Test
{ {
Test() Test()
{ {
}
super();
} }
}
---------------------------------------------------------------------------------
-----------------------
class Test class Test
{ {
Test(int i) Test(int
i)
{
this(); {
this();
}
Test() }
{
} Test()
} {
super();
}
}
---------------------------------------------------------------------------------
-------------------------
class Test class Test
{ {
Test(int i) Test(int
i)
{
super(); {
}
}
super();
}
}
---------------------------------------------------------------------------------
---------------------
various cases of super() and this()
The first line inside the constructor is either super() or this()
case 1:
class Test
{
Test()
{
[Link]("Constructor");
super();
}
}
// The compiler will give error because super() should be the first line inside
the constructor.
Compile Time Error: call to super must be first statement in constructor
case 2:
Use of super() or this() as a first line inside a constructor.
class Test
{
Test()
{
super();
this();
[Link]("Constructor");
}
}
// The compiler will give error because super() and this() should be the first
line inside the constructor. But here this() is in the second line and hence
error.
Compile Time Error: call to this must be first statement in construtor.
case 3:
class Test
{
public void Test()
{
super();
[Link]("Method");
}
}
// We can use super() or this() inside a constructor. Here it is used inside a
method.
Compile Time Error: call to super must be first statement in construtor.
Usage of super() and this() : used to call super class constructor and current
class constructor.
1. We use only inside the constructors.
2. Use only in first line.
3. We can use either super() or this() but not both simultaneously.
---------------------------------------------------------------------------------
-------
Differences between super() and this(), super and this
super and this are used to refer super class current variables or instance
members and this class current variables or instance members.
class P
{
String s = "parent variable";
}
class C extends P
{
String s = "child variable";
public void m1()
{
[Link](s); // child variable
[Link](this.s); // child variable
[Link](super.s); // parent variable
}
}
class Test
{
C c = new C();
c.m1();
}
super and this keywords can be used anywhere, except for static modifier.
class P
{
String s = "parent variable";
}
class C extends P
{
String s = "child variable";
public static void m1()
{
[Link](s); // child variable
[Link](this.s); // child variable
[Link](super.s); // parent variable
}
}
class Test
{
C c = new C();
c.m1();
}
Super(), this() Super, this
1. These are constructor calls to call super class and current class
constructors 1. These are the keybowrds to refer super class and current
instance members
2. We can use only in constructor as first statement
2. We use anywhere except static area
3. We can use only one, but not both simultaneously
3. We can use any number of times
-----------------------------------------------------------
overloaded constructors
class Enquiry
{
Enquiry(String name, long rno, String mailid, String address, String
course)
{
}
Enquiry(String name, String mailid)
{
}
Enquriy(String name)
{
}
}
we can write any number of constructors inside a class.
Constructors having same name but with different arguments are called overloaded
constructors.
class Test
{
Test(double d)
{
this(10); // int argument constructor
Sopln("double arg constructor");
}
Test(int i)
{
this(); // no argument constructor
sopln("int arg constructors");
}
Test()
{
sopln("no arg constructor");
}
psv main(String[] args)
{
Test t1 = new Test(10.5);
// no arg constructor
//int arg constructor
//double arg constructor
Test t2 = new Test(10);
//no arg constructor
//int arg constructor
Test t3 = new Test();
// no arg constructor
}
}
-------------------------------------------------------------
Constructor Inheritance and Overriding
Inheritance and Overriding concepts are not applicable for constructors.
class P
{
public void m1()
{
}
}
class C extends p
{
public void m2()
{
}
}
C c = new C();
c.m1();
c.m2();
class P
{
P()
{
}
}
class C extends p
{
C(int i)
{
}
}
C c = new C();
we will get compile time error.
constructors are not applicable to interfaces.
they are applicable to concrete classes and abstract classes(to initialise
instance variables if any).
-----------------------------------------------
Recursive Method and Constructor Calls
3 imp cases related to constructors
case 1:
class Test
{
public static void m1()
{
m2();
}
public static void m2()
{
m1();
}
psv main(String[] args)
{
m1(); // NO Compile time error but Runtime error: StackOverflowError
sopln("Hello");
}
}
class Test
{
Test()
{
this(10);
}
Test(int i)
{
this();
}
psvm(String [] args)
{
sopln("hello");
}
}
compile time error: recursive constructor invocation
-------------------------------------------------
Parent and Child class constructor calls by super()
valid
-------------------
class P
{
}
class C extends P
{
}
valid
---------------------
class P
{
P()
{
}
}
class C extends P
{
}
invalid
------------------------
class P
{
P(int i)
{
}
}
class C extends P
{
}
}