You are on page 1of 5

CONSTRUCTORS

What is a constructor?
A Constructor is a member method of a class which has the same name as that of a
class with no return type. It is used to initialize the instance variable of a class with a legal
initial value and gets involved automatically as soon as the object of a class is created.
Example:
class ConsExamp
{
int n;
ConstExamp()
{
n=10;
}
Public static void main(String arg[])
{
ConsExamp c=new ConstExamp();
System.out.println(c.n);
}
}
Output:
10
In this example ConstExamp is the Class with constructor to initialize the variable n
value as 10. In the main method the value to n is initialized when the object c is created using
the Constructor. The value of n is accessed using the object name and the dot operator and
displayed.
Need of Constructor
 To create an instance of a class
 To initialize objects of that class type with a legal initial values.
How to Invoke a Constructor?
A constructor is created when we create the object of a class.
How an object is Created?
The object is created using the new operator.
Syntax: -
<class name> <object name> = new <constructor>
Example: -
Student s= new Student();

Class Object new Constructor


Operator
Using the new operator with the Student constructor s object is created and variables
get initialized.
List out the rules to write a constructor.
What are the rules of a constructor?
1. Constructor should have the same class name in which it resides.
2. A constructor cannot be abstract, final, static and synchronized.
3. Access modifiers can be used in the Constructor. Probably public access modifiers are
used in constructors.
Why constructor should be defined public?
What access modifier does constructor use? Why?
A constructor should be defined public due to the following reasons.
1. The object can be created in any method.
2. A constructor might be called from outside the class while creating an object.
What are the function of a constructor?
What are the purpose of a constructor?
Prove a constructor is a member method that can be automatically be called while
creating an object to initialize its elements.
Show that a constructor is a member method that can be automatically be called while
creating an object to initialize its elements.
When we create various objects of a class, the data members are automatically
allocated under each object. To have separate initial values for each object, one may have to
use a member method to initialize the data members and call it separately every time after
creating an object.
Differentiate Constructors and Methods.
Constructors Methods
1 It has the same name as that of the class It has any name other than the class name
Name
2 It has no return type. It has a valid return type
3 They are called at time of object creation They are called when a function for the method
is encountered
4 Doesn’t require to be called in the main class It is called from the main method
5 It is used to initialize instance variables of a It is used to perform a particular task
class
6 They are usually public Any access specifiers can be used

List out the features of constructors.


What are the features of constructors?
1. A constructor has the same name as that of the class name. This allows the constructor
to be invoked automatically on object creation.
2. Constructor is automatically called while creating an object. When an object is
created, the constructor is called implicitly.
3. Constructor has no return type, not even void. A constructor is only used to initialize
the data members. No arithmetical or logical operations are performed in a
constructor. Hence, the return from constructor is not at all required.
4. Constructor is used to initialize the data members or instance variable of a class.
5. Constructor is always public.
6. Constructor is overloaded automatically.
Only a portion that has access to the constructor can use objects of that class. Comment.
Every time when object is created, it is automatically initialized by constructor of
class. Therefore, it is very much necessary for a function using an object that it must have
access to the constructor of the class so that the object being defined could be properly
constructed.
What are the types of Constructor?
Write notes on types of Constructor.
Three types of constructor exist in Java. They are
1. Default Constructor or non-parameterized constructor
2. Parameterized Constructor
3. Copy Constructor
1) Default Constructor or non-parameterized constructor
 A constructor that has no parameters is called as Default Constructor or non-
parameterized constructor.
 The signature is same as default constructor, however body can have any code
unlike default constructor while the body does nothing.
 The non-parameterized constructor used to initialize the instance variables to
zero, null or any default values without specifying is known as default
constructor.
 If any constructor is not explicitly defined, then the compiled uses the default
constructor.
Example: - Non-parameterized constructor
Public class Student
{
String name;
Student()
{
name=”Kala”;
}
Public static void main(String ar[])
{
Student s=new Student();
System.out.println(s.name);
}
}
Output: -
Kala
Here without any parameters passed the data member name value is initialized
as “Kala”
Example: - Default Constructor
Public class Student
{
String name;
Public static void main(String ar[])
{
Student s=new Student();
System.out.println(s.name);
}
}
Output: -
““
Here constructor is not explicitly defined. So the default value of string data
type “” is initialized.
2) Parameterized Constructor: -
 It is a member function with same name as that of a class name which is used
to initialize the instance variables by passing parametric values at the time of
its creation.
 It allows us to initialize the various data elements of different objects with
different values where they are created.
 This is achieved by passing different values as arguments to the constructor
when the objects are created.
 Defining a constructor with arguments hides the default constructor.
 With a parameterized constructor for a class, one must provide initial values as
arguments.
Example: - Parameterized constructor
Public class Student
{
String name;
Student(String nam)
{
name=nam;
}
Public static void main(String ar[])
{
Student s=new Student(“Kala”);
System.out.println(s.name);
}
}
Output: -
Kala
3) Copy Constructor: -
 A copy constructor initializes the instance variables of an object by copying
the initial values of the instant variables from another object.
 It can be done in two ways
1. Without using Specific constructor.
2. By passing object as parameters.
Example: -
Without using Specific constructor.
Student s=new Student();
Student s1=s;
By passing object as parameters.
Class Student
{
String name;
Student(String nam)
{
name=naml
}
Student(Student stu)
{
name=stu.name;
}
Public static void main(String ar[])
{
Student s=new Student(“Kala”);
Student stu=new Student(s);
}
}

You might also like