Constructor in java is a special type of method that is used to initialize the object.
Java constructor is invoked at the time of object creation. It constructs the values i.e. provides data for the object that is why it is known as constructor.
Rules for creating java constructor
There are basically two rules defined for the constructor.
- Constructor name must be same as its class name
- Constructor must have no explicit return type
Types of java constructors
There are two types of constructors:
- Default constructor (no-arg constructor)
- Parameterized constructor
Default Constructor
A constructor is said to be default constructor if and only if it never take any parameters.
If any class does not contain at least one user defined constructor than the system will create a default constructor at the time of compilation it is known as system defined default constructor.
Syntax
class className { ..... // Call default constructor clsname () { Block of statements; // Initialization } ..... }
Note: System defined default constructor is created by java compiler and does not have any statement in the body part. This constructor will be executed every time whenever an object is created if that class does not contain any user defined constructor.
Example of default constructor.
In below example, we are creating the no argument constructor in the Test class. It will be invoked at the time of object creation.
Example
//TestDemo.java class Test { int a, b; Test () { System.out.println("I am from default Constructor..."); a=10; b=20; System.out.println("Value of a: "+a); System.out.println("Value of b: "+b); } }; class TestDemo { public static void main(String [] args) { Test t1=new Test (); } };
Output
Output: I am from default Constructor... Value of a: 10 Value of b: 20
parameterized constructor
If any constructor contain list of variable in its signature is known as paremetrized constructor. A parameterized constructor is one which takes some parameters.
Syntax
class ClassName { ....... ClassName(list of parameters) //parameterized constructor { ....... } ....... }
Syntax to call parametrized constructor
ClassName objref=new ClassName(value1, value2,.....); OR new ClassName(value1, value2,.....);
Example of Parametrized Constructor
class Test { int a, b; Test(int n1, int n2) { System.out.println("I am from Parameterized Constructor..."); a=n1; b=n2; System.out.println("Value of a = "+a); System.out.println("Value of b = "+b); } }; class TestDemo1 { public static void main(String k []) { Test t1=new Test(10, 20); } };
Java Copy Constructor
There is no copy constructor in java. But, we can copy the values of one object to another like copy constructor in C++. There are many ways to copy the values of one object into another in java. They are:
|
Output:
111 Karan 111 Karan
Copying values without constructor
We can copy the values of one object into another by assigning the objects values to another object. In this case, there is no need to create the constructor.
Output:
111 Karan
111 Karan
111 Karan 111 Karan
Difference between Method and Constructor
Method | Constructor | |
---|---|---|
1 | Method can be any user defined name | Constructor must be class name |
2 | Method should have return type | It should not have any return type (even void) |
3 | Method should be called explicitly either with object reference or class reference | It will be called automatically whenever object is created |
4 | Method is not provided by compiler in any case. | The java compiler provides a default constructor if we do not have any constructor. |
No comments:
Post a Comment