Java Placement tutorial | What is constructor in Java | what is default constructor | What is parameterised constructor

 Java Placement tutorial |  What is a constructor in Java | what is a default constructor | What is parameterized constructor 



Java Constructor is a special method that doesn't have a return type.

The name of the constructor should be same as name of the class.

There are two types of constructor 

1. Default constructor


It is already there whenever you create the object of any class without any parameter

public class Test {

int a;

Test() {

}

}

int this program Test() is the default constructor. All the default assignment for instance variable happens in the default constructor.

in the above example for a, 0 will be assigned as the default value.


2. Parameterized constructor


If we are passing any param in the constructor then that is called a parameterized constructor.

public class Test {

int a;

Test(int input) {

a=input

}

}





Post a Comment

0 Comments