Java Constructor is a block just like a method used for object initialization. It is a special method invoked by new operator at the time of instance creation. Java Constructor will have default constructor as well as parameterized constructor. Following is the syntax to declare the constructor.
[java]constructorName(args)
{
------------
-----------
return;
}[/java]
Java Constructor name should be same as class name.
Java Constructor shouldn’t be declared using return type.
Java Constructor shouldn’t have a return value.
For a Constructor if return type is defined, Java treats it as a method.
To create an instance of a class, the class should provide a Constructor, otherwise the JVM cannot create an instance of the class.
A constructor can invoke only one constructor.
Recursive constructor invocation is not supported in Java,where as recursive method invocation is supported.
Constructors are not loaded into the heap (memory) and when it will be called, it directly comes to stack and executes.
Default Constructor
Description
If the developer can not mention parameters in constructor then it becomes default constructor. Following is the syntax for the default constructor.
[java]
class splessons
{
splessons()//where splessons is the constructor.
}[/java]
Example
defaultconstructor.java
[java]
public class defaultconstructor {
defaultconstructor()
{
System.out.println("Hello welcome to SPlessons.");
}
public static void main(String args[])
{
defaultconstructor d=new defaultconstructor();
}
}
[/java]
Output:
When compile the code following is the output will be generated.
[c]
Hello welcome to SPlessons.
[/c]
Parameterized Constructor
Description
If developer pass any arguments through the constructor then it becomes Parameterized Constructor. Following is an example.
Example
Following is an example where arguments are passing through the constructor.
Splesson.java
[java]
public class Splesson {
int id;
String name;
Splesson(int i,String n){
id = i;
name = n;
}
void display(){System.out.println(id+" "+name);}
public static void main(String args[]){
Splesson s1 = new Splesson(100,"sam");
Splesson s2 = new Splesson(111,"Aryan");
s1.display();
s2.display();
}
}
[/java]
Output:
When compile the code following is the output will be generated.
[c]
100 sam
111 Aryan
[/c]
Constructor Overloading
Description
Developing more than one constructor in java class with different argument list is known as Constructor Overloading.
To achieve constructor overloading the arguments should differ in any one of the following.
1.Argument type
2.Argument length
3.Argumrnt position
If one tries to develop with the same argument list then compiler throws error.
Whenever the object member is to be initialized with different arguments, then go for developing overloading constructor.
Example
[java]
package com.spl.co;
public class ConstructorOverloading {
public static void main(String[] args) {
System.out.println("Program starts");
Demo dRef1=new Demo(5);
System.out.println("k value:"+dRef1.k);
Demo dRef2=new Demo(5.2);
System.out.println("d value:"+dRef2.d);
Demo dRef=new Demo(5,5.2);
System.out.println("k value:"+dRef.k);
System.out.println("d value:"+dRef.d);
System.out.println("Program ends");
}
}
class Demo
{
int k;
double d;
//overloaded constructor
Demo(int a)
{
System.out.println("Running Demo(int) constructor");
k=a;
}
Demo(double b)
{
System.out.println("Running Demo(double) constructor");
d=b;
}
Demo(int a,double b)
{
System.out.println("Running Demo(int,double) constructor");
k=a;
d=b;
}
}
[/java]
Constructor overloading is a strategy in Java in which a class can have any number of constructors that vary in parameter lists.The compiler separates these constructors by considering the quantity of parameters in the rundown and their sort.
Output:
[c]
Program starts
Running Demo(int) constructor
k value:5
Running Demo(double) constructor
d value:5.2
Running Demo(int,double) constructor
k value:5
d value:5.2
Program ends[/c]
"This" keyword
Description
Java provides a special keyword to store the address of current object is caled "This". "This" keyword should be used only inside constructor body or non static context.
To differentiate local variable and object members "this" keyword can be used for referring object members.
Example
The following is an example without using this keyword.
[java]
package com.splessons;
class Student{
int rollno;
String name;
float fee;
Student(int rollno,String name,float fee){
rollno=rollno;
name=name;
fee=fee;
}
void display(){System.out.println(rollno+" "+name+" "+fee);}
}
class Splesson{
public static void main(String args[]){
Student s1=new Student(100,"john smith",6000f);
Student s2=new Student(112,"sumit",6000f);
s1.display();
s2.display();
}}
[/java]
Where instance variables and formal parameters are same, to show the variation between them this keyword will be used.
Output:
[c]
0 null 0.0
0 null 0.0
[/c]
The solution for the above code by using This keyword.
[java]package com.splessons;
class Student{
int rollno;
String name;
float fee;
Student(int rollno,String name,float fee){
this.rollno=rollno;
this.name=name;
this.fee=fee;
}
void display(){System.out.println(rollno+" "+name+" "+fee);}
}
class Splesson{
public static void main(String args[]){
Student s1=new Student(100,"john smith",6000f);
Student s2=new Student(112,"sumit",6000f);
s1.display();
s2.display();
}} [/java]
In any case formal parameters and instance parameters are different then no need to use This keyword.
Output:
[c]
100 john smith 6000.0
112 sumit 6000.0
[/c]
"This" statement
Description
A constructor of the class can invoke another constructor of the same class using "this" statements.
"this" statement must be the first statement inside the constructor .
In a constructor body, two "this" statements cannot be used.
"this" statement should be used only inside the constructor.