29.constructor in Java
29.constructor in Java
Vishnu
Constructor in java
Constructor:
Constructor is a special member of a class which is used to initialize the state of an
object. It provides the values to the data members at the time of object creation that is
why it is known as constructor.
Characteristics of constructor:
1. A constructor must have the same name as of its class.
2. It is invoked at the time of object creation and used to initialize the state of an object.
3. It does not have an explicit return type.
Types of constructor:
1. Default or no-argument constructor.
2. Parameterized constructor.
Default or no-argument constructor:
A constructor with no parameter is known as default or no-argument constructor. If no
constructor is defined in the class then compiler automatically creates a default
constructor at the time of compilation.
Syntax:
Class_name(){
//optional block of code
}
Why default constructor is used?
Default constructor is used to provide default values to the object properties i.e. to
provide default state of an object.
Example:
package com.sst;
1
Sri Sureka Technologies, Opposite Geethanjali High School, Near S.R.Nagar Signal, S.R.Nagar,
Hyderabad-500038, Ph: 040-66616677, Mobile: +91-9885602332.
29. Constructor By Mr. Vishnu
int num;
String str;
ConstructorExample1() {
System.out.println("Constructor called.");
}
Output:
Constructor called.
num = 0
str = null
packagecom.sst;
2
Sri Sureka Technologies, Opposite Geethanjali High School, Near S.R.Nagar Signal, S.R.Nagar,
Hyderabad-500038, Ph: 040-66616677, Mobile: +91-9885602332.
29. Constructor By Mr. Vishnu
Output:
num = 0
str = null
Parameterized constructor:
A constructor with one or more arguments is known as parameterized constructor.
Why parameterized constructor is used?
Parameterized constructor is used to provide values to the object properties. By use of
parameterized constructor different objects can be initialize with different states.
Example:{
packagecom.sst;
ConstructorExample3(int n, String s) {
System.out.println("Constructor called.");
num = n;
str = s;
}
3
Sri Sureka Technologies, Opposite Geethanjali High School, Near S.R.Nagar Signal, S.R.Nagar,
Hyderabad-500038, Ph: 040-66616677, Mobile: +91-9885602332.
29. Constructor By Mr. Vishnu
Output:
Constructor called.
num = 10
str = javawithease
packagecom.sst;
int num;
String str;
ConstructorExample4(int n, String s) {
System.out.println("Constructor called.");
num = n;
str = s;
}
4
Sri Sureka Technologies, Opposite Geethanjali High School, Near S.R.Nagar Signal, S.R.Nagar,
Hyderabad-500038, Ph: 040-66616677, Mobile: +91-9885602332.
29. Constructor By Mr. Vishnu
// constructor call
ConstructorExample4 obj1 = new ConstructorExample4(10, "sst");
Output:
packagecom.sst;
isStudent = boolean1;
}
Output:
One argument constructor called.
isStudent = true
num = 0
str = null
Two argument constructor called.
isStudent = false
num = 10
str = vishnu
Three argument constructor called.
isStudent = false
num = 20
str = vishnu.com
7
Sri Sureka Technologies, Opposite Geethanjali High School, Near S.R.Nagar Signal, S.R.Nagar,
Hyderabad-500038, Ph: 040-66616677, Mobile: +91-9885602332.
29. Constructor By Mr. Vishnu
How to copy the values one object into another object using constructor?
Example:
packagecom.sst;
ConstructorExample6(intn, String s) {
System.out.println("Constructor called.");
num = n;
str = s;
}
}
8
Sri Sureka Technologies, Opposite Geethanjali High School, Near S.R.Nagar Signal, S.R.Nagar,
Hyderabad-500038, Ph: 040-66616677, Mobile: +91-9885602332.
29. Constructor By Mr. Vishnu
Output:
Constructor called.
obj1 num = 10
obj1 str = vishnu
Constructor called for copying value.
obj2 num = 10
obj2 str = vishnu
9
Sri Sureka Technologies, Opposite Geethanjali High School, Near S.R.Nagar Signal, S.R.Nagar,
Hyderabad-500038, Ph: 040-66616677, Mobile: +91-9885602332.