Java Constructors:
Constructor: is a special method which is used to initialize object. Constructor similar to the methods declarations. This will be invoked during object initialization.
Thump rules for Constructor:
1) Constructor name should same as Class name.2) Should not have any return type, even void also not allowed.
3) Can not be applied static, abstract, final on constructor.
4) Constructor should not be duplicated. Always should be change in parameters.
Constructor Purpose:
The purpose of the constructor is to initialize the properties to its default values or customized values.
Constructor types in Java:
Constructor are defined as two types.1) Default parameter(no-argument or zero Parameterized constructor)
2) Parameterized constructor
1) Default parameter:
Default parameter is nothing but a zero argument parameter. This is invoked whenever we create object.
Example:
case 1:
Output:
package com.java.w3schools.core.constructor;
public class Chair {
public Chair() {
System.out.println("Constructor is invoked.");
}
public static void main(String[] args) {
Chair chair1 = new Chair();
Chair chair2 = new Chair();
}
}
Constructor is invoked.
Constructor is invoked.
As per the output, looks constructor has executed twice.
Guess. What might be the reason. Because we have created object two times for class chair. So, Whenever we create object, Constructor will be invoked.
If we create 100 objects for Chair class than 100 times constructor will be invoked.
This default constructor is optional. We no need to create. If we do not create it, compiler is responsible to create it.
Parameterized Constructor:
A constructor which has parameters is called "Parameterized Constructor". We can have multiple constructors in one class. All constructors names should be same as class name. This can be overloaded.
Purpose :
This is to initialize the object with different values for different objects.
Example:
case 2:
package com.java.w3schools.core.constructor;
public class Chair {
public Chair(String name) {
System.out.println("Parameterized Constructor is invoked.");
}
public static void main(String[] args) {
Chair chair1 = new Chair("Chair 1");
Chair chair2 = new Chair("Chair 2");
}
}
Output:
Parameterized Constructor is invoked.
Parameterized Constructor is invoked.
Here we have not initialized properties in constructor.
case 3:
package com.java.w3schools.core.constructor;
public class Chair {
public String chairName;
public Chair(String name) {
chairName = name;
}
public static void main(String[] args) {
Chair chair1 = new Chair();
Chair chair2 = new Chair();
}
}
Output:
Compile time error.
Reason : The constructor chair() is undefined.
In this case compiler will not create default create for us. We have to create it or have to use parameterized constructor.
case 4:
package com.java.w3schools.core.constructor;
public class Chair {
public String chairName;
public Chair(String name) {
chairName = name;
}
public String getName() {
return chairName;
}
public static void main(String[] args) {
Chair chair1 = new Chair("Chair 1");
Chair chair2 = new Chair("Chair 2");
System.out.println("chair1 name :: " + chair1.getName());
System.out.println("chair2 name :: " + chair2.getName());
}
}
Here, setting name to each object using parameterized constructor.
Output:
chair1 name :: Chair 1
chair2 name :: Chair 2
case 5:
package com.java.w3schools.core.constructor;
public class Chair {
public String chairName;
public double weight;
public Chair(String name) {
chairName = name;
}
public Chair(String name) {
chairName = name;
}
public String getName() {
return chairName;
}
public static void main(String[] args) {
Chair chair1 = new Chair("Chair 1");
Chair chair2 = new Chair("Chair 2");
System.out.println("chair1 name :: " + chair1.getName());
System.out.println("chair2 name :: " + chair2.getName());
}
}
Output:
Compile time error.
Reason: Duplicate method Chair(String) in type Chair.
Constructor can not be duplicated.
case 6:
package com.java.w3schools.core.constructor;
public class Chair {
public String chairName;
public double chairWeight;
public Chair(String name) {
chairName = name;
}
public Chair(String name, double weight) {
chairName = name;
chairWeight = weight;
}
public String getName() {
return chairName;
}
public double getWeight() {
return chairWeight;
}
public static void main(String[] args) {
Chair chair1 = new Chair("Chair 1");
Chair chair2 = new Chair("Chair 2", 45);
System.out.println("chair1 name :: " + chair1.getName());
System.out.println("chair1 weight :: " + chair1.getWeight());
System.out.println("chair2 name :: " + chair2.getName());
System.out.println("chair2 weight :: " + chair2.getWeight());
}
}
We have set the name only to chair1, set both to chair2.
output:
chair1 name :: Chair 1
chair1 weight :: 0.0
chair2 name :: Chair 2
chair2 weight :: 45.0
Chair1 weight has initialized to zero and for chair2 is 45.
If we do not set properties, than will be initialized to it's default values.
0 Comments