Previous | Home | Next |
- Constructor is a member method.
- It is called automatically when we create an object.
- Its name is same as class name.
- It should be defined in public block because called outside the class but it can defined private if called inside the class.
- A class can contain more than one constructor if there argument are different and this is called as constructor overloading.
- Every class can contain a default zero argument constructor if no explicit constructor is defined by us.
- In c++ constructor also provides memory to class member variables inside the object but in java it is the new operator which provide memory to class member variables inside the objects and new also initialize the member variable.
- Constructor is not void it implicitly return object of type itself, We can't explicitly return any value from the constructor.
- Constructor’s syntax does not include a return type, since constructors never return a value.
- New is a keyword as well as operator in java.
- If you declare a class with no constructors, the compiler will automatically create a default constructor for the class. A default constructor takes no parameters and has an empty body. Because the compiler will automatically generate a default constructor if you don't declare any constructors explicitly, all classes have at least one constructor.
Example :
Types of java constructors
There are two types of constructors:
- Default constructor (no-arg constructor)
- Parameterized constructor
Java Default Constructor
Every class has a default constructor (if no explicit constructor) that does not take any argument and its body does not have any statements. The compiler generate the default constructor automatically. The compiler stops genereting default constructor as soon as we add our own constructor. When we do not create any constructor in the class the jvm will create the default constructor and initialize the instance variable with default values null or zero.
A constructor that have no parameter is known as default constructor.
Default constructor provides the default values to the object like 0, null etc. depending on the type.
Syntax :
<class_name>() { }
Example :
class A { private int x; private int y; void display() { System.out.print(x+"\t"+y); } class Test { public static void main(String args[]) { A a1=new A(); a1.display(); } }
output :
0 0
Note :Here we have not created any constructor then jvm create automatically default constructor after creating object and initialize the instance variable from 0 or null.
Parameterized constructor
A constructor that have parameters is known as parameterized constructor.
Parameterized constructor is used to provide different values to the distinct objects.
Example :
class A { private int x,y; A()//default zero argument constructor { this.x=y=5; } A(int x1)//parametrized one argument constructor { x=y=x1; } A(int x1,int y1) { x=x1; y=y1; } void display() { System.out.print(x+","+y); } } class ATest { public static void main(String args[]) { A a1; a1=new A(); a1.display(); A a2; a2=new A(5); a2.display(); A a3; a3=new A(5,6); a3.display(); } }
output :
5,5 5,5 5,6
Previous | Home | Next |