Java Constructor

Dhanuka lakshan
2 min readOct 28, 2020

Java constructor is a special method in java.The construcor duty is initiallize the object(non-static).It is called when instance of the class is created.If you not mention the constructor compliler add the default constructor put the byte code by default.Java support the constructor overloading.We create multiple constructors with the same name but with different parameters types.

There are two rules defined for the constructor. First rule is constructor name must be class name. A construcor no have explicit return type and java constructor can not be abstract , static , final and synchronized.

Class Student{Student(){//constructor}}

There are two types of constructor which are default constructor(no-arg constructor) and parameterized constructor(full-arg constructor).How the default constructor works?

class Student{
int a;
Student(){ //default constructor
System.out.println("a values is "+a);
}
/*compiler code insert
Student(){
int a;
a=0;
System.out.println("a values is "+a);
}
*/
}
class Demo{
public static void main(String[] at){
Student s = new Student();
}
}

How the the parameter construcor works?

class Student{
String name;
int age;
Student(String name,int age){//parameterized constructor(full-arg construcor)
this.name=name;
this.age=age;
System.out.println("Student name is "+ name +" his age is "+ age);
}
/*compiler insert code
Class Student{Student(String name,int age){

name="Dhanuka Lakshan.";
age = 20;
System.out.println("Student name is "+ name +" his age is "+ age);
}
*/
}
class Demo{
public static void main(String[] at){
Student s = new Student("Dhanuka Lakshan.",20);
}
}

If you mention the the constructor, compiler not add the default constructor in the bytecode.The default constructor is used to provide the default values to the object like 0, null, etc., depending on the type.

class A{
int a;
int b;
A(int a,int b){
this.a = a;
this.b=b;
System.out.println("a :"+a +" b :"+b);
}
}
class Demo{
public static void main(String[] at){

A a1 = new A(10,20);
A a2 = new A(); //compile error
}
}

More and one constructors may be had the each class. After created the object, the construcor can not be called.

class A{
int a;
int b;
A(){}
A(int a,int b){
this.a = a;
this.b=b;
System.out.println("a :"+a +" b :"+b);
}
}
class Demo{
public static void main(String[] at){

A a1 = new A();
a1.A(10,20);//compile error

}
}

If we declared variables,it is copied to the begining of the construtor.

class A{
int a=10;
int b=20;
A(){a=100;}

A(int a){
this.a =a;
}

A(int a,int b){}
/*compiler insert code
Class A{
int a;
int b;
A(){
int a=10;
int b=20;
a=100;
}

A(int a){
int a=10;
int b=20;
int a=1000;
}
A(int a,int b){
int a=10;
int b=20;
}
}
*/
}
class Demo{
public static void main(String[] at){

A a1 = new A();
System.out.println("a :"+a1.a +" b :"+a1.b);//100 20

A a2 = new A(1000);
System.out.println("a :"+a2.a +" b :"+a2.b);//1000 20

A a3 = new A(1000,2000);
System.out.println("a :"+a3.a +" b :"+a3.b);//10 20

}
}

--

--

Dhanuka lakshan

I’m a Software engineering student who is currently studying @Institute_of_Java And Software Engineering(IJSE), I like coding and learn about new tech.