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.
- Constructor is used in the creation of an object
- It is the block of code used to initialize an object
- Constructor gets executed when an object of a class is being created
- Special type of method that is used to initialize the object is called constructor.
- During the object creation only, constructor is executed.
- If you want to perform the particular task during object creation, then write the task by using constructor and this constructor part will be executed during object creation
- Inside the class, if you are not declaring any constructor, then only compiler will do it for. If you are declaring at least one constructor, then compiler will not generate any constructor.
- this() and super() must be the first statement in the constructor code. If you do not mention them, compiler does it for you
- Constructor can not be inherited
- A constructor can invoke another constructor of same class by using this().
class Test {
}
Test t = new Test();
Test............class name
t...................reference variable (object name)
= ................assignment operator
new............keyword used to create object
Test()..........constructor
Rules to declare constructor
- Constructor name and class name should be same
- Constructor is able to take parameters
- Return type is not allowed
Types of constructor:
- Default constructor (0-arg constructor with empty implementation): Inside the class, if you are not declaring any constructor, then compiler generate the 0-argument constructor with empty implementation, called default constructor
Example 1:
class Test1{
void m1() {
System.out.println("m1 method");
}
/*Test1(){ // default constructor
}*/
}
public class ConstructorDemo1 {
public static void main(String[] args) {
Test1 t = new Test1();
t.m1();
}
}
- User defined constructor
Example 2:
package com.cerotid.constructor;
class Test2{
void m1() {
System.out.println("m1 method");
}
Test2(){
System.out.println("0-argument constructor");
}
Test2(int a){
System.out.println("1-argument constructor");
}
}
public class ConstructorDemo2 {
public static void main(String[] args) {
Test2 t = new Test2();
Test2 t1 = new Test2(10);
t.m1();
t1.m1();
}
}
Output:
0-argument constructor
1-argument constructor
m1 method
m1 method
Example 3: Inside the class, if you are not declaring any constructor, then only compiler generate default constructor. If you are declaring at least one constructor, then compiler will not generate any constructor. During the compilation it checks 0-arg constructor, it is not available and error occurs
class Test3{
void m1() {
System.out.println("m1 method");
}
Test3(int a){
System.out.println("1-argument constructor");
}
}
public class ConstructorDemo3 {
public static void main(String[] args) {
Test3 t = new Test3(10);
//Test3 t1 = new Test3(); //this line gives error as there in no 0-argument constructor
t.m1();
}
}
Advantages of constructor:
Advantage # 1: Constructors are used to write the business logics of the application and those logics are executed during object creation (25%):
Example 4:
class Test4{
Test4(){
System.out.println("0- argument constructor");
}
}
public class ConstructorDemo4 {
public static void main(String[] args) {
Test4 t = new Test4(); //
}
}
Output:
0-arg constructor
Advantage # 2: (75%) In fact, Java requires a constructor call for every object that is created. Keyword new calls the class's constructor to perform the initialization. The constructor call is indicated by the class name followed by parentheses—the constructor must have the same name as the class name. When a class has only the default constructor, its instance variables are initialized to their default values. Variables of types char, byte, short, int, long, float and double are initialized to 0, variables of type boolean are initialized to false, and reference-type variables are initialized to null.
Constructors are used to perform initialization of instance variable during object creation time. When you declare a class, you can provide your own constructor to specify custom initialization for objects of your class. For example, you might want to specify a course name for a GradeBook object when the object is created, as in GradeBook myGradeBook = new GradeBook( "CS101 Introduction to Java Programming" );
In this case, the argument "CS101 Introduction to Java Programming" is passed to the GradeBook object's constructor and used to initialize the courseName. The preceding statement requires that the class provide a constructor with a String parameter. Figure 3.10 contains a modified GradeBook class with such a constructor.
Example 5: In this example, there is a problem (only default values are printed even object is created)
case 1:
class Test5{
int eid;
String ename;
double esalary;
void display() {
System.out.println("Employee id: "+eid);
System.out.println("Employee ename: "+ename);
System.out.println("Employee id: "+esalary);
}
}
public class ConstructorDemo5 {
public static void main(String[] args) {
Test5 t = new Test5(); // default constructor is executed
t.display();
}
}
Output:
Employee id: 0
Employee ename: null
Employee id: 0.0
Case II:In order to solve above problem, we need to use user defined constructor.User defined constructors are executed during object creation. Bellow example is user defined (0-argument constructor).
class Test6{
int eid;
String ename;
double esalary;
Test6(){
eid = 111;
ename = "David";
esalary = 100000.24;
}
void display() {
System.out.println("Employee id: "+eid);
System.out.println("Employee ename: "+ename);
System.out.println("Employee salary: "+esalary);
}
}
public class ConstructorDemo6 {
public static void main(String[] args) {
Test6 t = new Test6();
t.display();
Test6 t1 = new Test6();
t1.display();
}
}
Output:
Employee id: 111
Employee ename: David
Employee salary: 100000.24
Employee id: 111
Employee ename: David
Employee salary: 100000.24
Case III:In order to solve above problem, we need to use user defined (parameterized constructor).
class Test7{
int eid;
String ename;
double esalary;
Test7(int eid, String ename, double esalary){
}
void display() {
System.out.println("Employee id: "+eid);
System.out.println("Employee ename: "+ename);
System.out.println("Employee salary: "+esalary);
}
}
public class ConstructorDemo7 {
public static void main(String[] args) {
Test7 t = new Test7(111, "David", 100000.24);
t.display();
Test7 t1 = new Test7(222, "Linda", 200000.48);
t1.display();
}
}
Output:
Employee id: 0
Employee ename: null
Employee salary: 0.0
Employee id: 0
Employee ename: null
Employee salary: 0.0
Case IV: very very important (Use of user defined parameterized constructor and convert the local variable to instance variable)
class Test8{
int eid;
String ename;
double esalary;
Test8(int eid, String ename, double esalary){ // convert the local variable to instance variable
//conversion of local values to instance values
this.eid = eid;
this.ename = ename;
this.esalary = esalary;
}
void display() {
System.out.println("Employee id: "+eid);
System.out.println("Employee ename: "+ename);
System.out.println("Employee salary: "+esalary);
}
}
public class ConstructorDemo8 {
public static void main(String[] args) {
Test8 t = new Test8(111, "David", 100000.24);
t.display();
Test8 t1 = new Test8(222, "Linda", 200000.48);
t1.display();
}
}
Output:
Employee id: 111
Employee ename: David
Employee salary: 100000.24
Employee id: 222
Employee ename: Linda
Employee salary: 200000.48
Constructor calling: Similar to, one method can call another method, one constructor can call another constructor. However, one constructor can call only one constructor at a time where as one method can call multiple method at a time. To call the constructor, use 'this' keyword and 'this' keyword must be the first statement in constructor. As a rule, constructors with fewer arguments should call those with more
Example I:
package com.cerotid.constructor;
class Test10{
Test10(){
this(10);
System.out.println("0-argument constructor");
}
Test10(int a){
this(10, 20);
System.out.println("1-argument constructor");
}
Test10(int a, int b){
System.out.println("2-argument constructor");
}
}
public class ConstructorDemo9 {
public static void main(String[] args) {
Test10 t1 = new Test10();
}
}
Output:
2-argument constructor
1-argument constructor
0-argument constructor
No comments:
Post a Comment