Introduction
Types of constructors
Non-parameterized constructors
A non-parameterized constructor in Java is a constructor without parameters. Non-parameterized constructors are also considered as default constructors. It is automatically generated by the compiler if no constructors are explicitly defined in the class. The default constructor provided by compiler initializes the data members by their default values.
Syntax
class Constructor {
public Constructor() {
//code
}
}
Parameterized Constructors
Parameterized constructors are the ones that receive parameters and initialize objects with received values.
Syntax
class Constructor {
int a;
public Constructor(int a) {
this.a = a;
}
}
Copy Constructors
A copy constructor is a constructor that initializes a new object using an existing object of the same class. This is useful when you want to create a copy of an object without modifying the original object.
Syntax
class Constructor {
int i;
public Constructor(Constructor c) {
this.i = c.i; //c is the another object of same Class
}
}
this keyword
The keyword this is a reference that refers to the currently called object. This reference is by default made available to the invoked member method.
The keyword this
- only pertains to an instance method, not to a class method. (i.e. cannot be used with static method(s) and also static variables)
- is an implicit argument to this instance of the class.
- is accessible inside of any instance method. (i.e. any non-static methods)
Use cases of this keyword
Using ‘this’ keyword to refer to current class instance variables
Code :
class Test {
int a = -1;
int b = -1;
Test(int a , int b) {
/*both are refering to the local variable/parameter
to the constructor*/
a = a;
/*this.b is refering to the instance variable b with value -1*/
this.b = b;
}
void print() {
System.out.print(a + "\n" + b);
}
}
class Main {
public static void main(String args[]) {
//creating temporary instance
new Test( 3 , 14 ).print();
}
}
Output :
-1
14
Using 'this' keyword to invoke current class constructor
Code :
class Test {
Test() {
//calling constructor of this class with parameters
this( 3 , 14 );
}
Test(int x , int y) {
System.out.print(x + "." + y);
}
}
class Main {
public static void main(String args[]) {
//creating temporary instance
new Test();
}
}
Output :
3.14
Using 'this' keyword to invoke current class methods
Code :
class Test {
Test() {
this.print( 3 , 14 );
}
void print(int x , int y) {
System.out.print(x + "." + y);
}
class Main {
public static void main(String args[]) {
//creating temporary instance
new Test();
}
}
Output :
3.14
Constructor Overloading
Just like methods, constructors can me overloaded so that even with different number and types of of initial values, an object may still be initialized.
Example
Code :
class Test {
Test() {
System.out.println("Hello from constructor 1");
}
Test(String s) {
System.out.println("Hello from Constructor 2");
}
Test(String s , String s2) {
System.out.println("Hello from Constructor 3");
}
}
class Main {
public static void main(String args[]) {
new Test();
new Test("argument1" , "argument2");
new Test("argument1");
}
}
Output :
Hello from Constructor 1
Hello from Constructor 3
Hello from Constructor 2