It is a block inside a class. Similar to method, a
constructor has codes but the difference is constructor never returns values ,
so does not have return type. The constructor name must always be similar to
the class name. You can supply input parameters
in the constructor , this way you can use multiple constructors inside a class,
this is called constructor overloading
which is discussed in the overloading method/constructor section.
Creating an object for a class , does not execute the codes
inside it. But when object for a constructer is created, the code inside the
constructor is executed.
To run a method , you need to create object for the class
and supply the object/memory reference to the method. As explained in example
below:
To run a method inside a class:
Let's create a
class and a method:
public class ConstructorTest {
public void constExample() {
System.out.println("This is a method inside a class.");
}
}
Let's run the method:
public static void main(String[] Args) {
ConstructorTest classObj = new ConstructorTest();
classObj.constExample();
}
OUTPUT:
This is a method inside a
class.
To run a Constructor:
Let's create a
constructor:
public class ConstructorTest {
public ConstructorTest() {
System.out.println("This is a statement inside a constructor.");
}
}
Let's run the
constructor:
public static void main(String[] Args) {
ConstructorTest constObj = new ConstructorTest();
}
OUTPUT:
This
is a statement inside a constructor.
Constructor overloading
You can use multiple
constructors inside a class( This is possible by supplying different input parameters to the constructor , called constructor overloading):
public class ConstructorTest {
public ConstructorTest() {
System.out.println("This is a constructor without parameter.");
}
public ConstructorTest(int x) {
System.out.println("This is a constructor with one parameter = "+x);
}
public ConstructorTest(int x, int y) {
System.out.println("This is a constructor with two parameters = "+x+" and "+y);
}
public ConstructorTest(int x, String
name) {
System.out.println("This is a constructor with different parameters = "+x+" and "+name);
}
}
Let's run the
constructors:
public static void main(String[] Args) {
ConstructorTest constObj = new ConstructorTest();
ConstructorTest constObj1 = new ConstructorTest(5);
ConstructorTest constObj2 = new ConstructorTest(5,6);
ConstructorTest constObj3 = new ConstructorTest(5,"David");
}
OUTPUT:
This is
a constructor without parameter.
This is
a constructor with one parameter = 5
This is
a constructor with two parameters = 5 and 6
This is a constructor with
different parameters = 5 and David
There is a problem in calling many constructors creating their objects. Creating object everytime we call a constructor will consume too much memory. So, to avoid this situation, we
can use "this" keyword to call constructor/s present in
the same class. This is called constructor chaining. Constructor chaining can be done in any order or to any
constructor or constructors within the same class. To use constructor chaining, there should be at
least one constructor without "this" keyword.
public class ConstructorTest {
public ConstructorTest() {
System.out.println("This is a constructor without parameter.");
}
public ConstructorTest(int x) {
this();
System.out.println("This is a constructor with one parameter = "+x);
}
public ConstructorTest(int x, int y) {
this(1);
System.out.println("This is a constructor with two parameters = "+x+" and "+y);
}
public ConstructorTest(int x, String
name) {
this(2,3);
System.out.println("This is a constructor with different parameters = "+x+" and "+name);
}
}
Let's run the
constructors:
public static void main(String[] Args) {
ConstructorTest constObj = new ConstructorTest(4,"Tina");
}
OUTPUT:
This is
a constructor without parameter.
This is
a constructor with one parameter = 1
This is
a constructor with two parameters = 2 and 3
This is a constructor with
different parameters = 4 and Tina
You can apply inheritance(click here to know more about inheritance) in constructor to call parent class
constructor from child class constructor.
Parent class:
public class ConstructorTest {
public ConstructorTest() {
System.out.println("This is a parent class constructor.");
}
}
Child Class:
public class ConstructorTest2 extends ConstructorTest {
public ConstructorTest2() {
System.out.println("This is a child class constructor.");
}
}
Let's run the child constructor:
public static void main(String[] Args) {
ConstructorTest2 constObj = new ConstructorTest2();
}
OUTPUT
This is
a parent class constructor.
This is a child class
constructor.
No comments:
Post a Comment