Java Constructor Tutorial
By AmarSivas | | Updated : 2019-10-28 | Viewed : 355 times

Constructors in Java allows us to
Table of Contents:
Constructors In Java
package com.docsconsole.corejava.apackage;
public class PersonAddress {
public PersonAddress() {
System.out.println("PersonAddress Constructor @PersonAddress");
}
public String personAddress() {
return "David, Road No 101, United States of America.";
}
public static void main(String[] args) {
PersonAddress address = new PersonAddress();
System.out.println("Address in Method@personAddress:: " + address.personAddress());
}
}
Here
Types Of Constructors
Based on the
Those are
-
Default Constructor
-
No-Args Constructor
-
Parameterized Constructor
Default Constructor:
It is not mandatory for us to provide the Constructor manually in Java class. When you not provide
package com.docsconsole.corejava.bpackage;
public class PersonAddress {
public static void main(String[] args) {
PersonAddress address = new PersonAddress();
}
}
Note: Java compiler will replace with
No-Args Constructor
When you implement Constructor with no arguments in Java then those type of constructor is called
package com.docsconsole.corejava.cpackage;
public class PersonAddress {
public PersonAddress() {
System.out.println("PersonAddress Constructor @PersonAddress");
}
public static void main(String[] args) {
PersonAddress address = new PersonAddress();
}
}
Here No Argument Constructor i.e.,
Parameterized Constructor
Constructor with arguments is called as
package com.docsconsole.corejava.dpackage;
public class Person {
private String firstName;
private String lastName;
public Person(String firstName, String lastName) {
//Parameterized Constructor
this.firstName = firstName;
this.lastName = lastName;
}
public static void main(String[] args) {
Person address = new Person("David", "Johnson");
System.out.println("First Name::: " +address.firstName + " ::: Last Name::: "+ address.lastName);
}
}
Constructor Overloading
Generally Overloading concept is applicable for Methods. But
package com.docsconsole.corejava.epackage;
public class PersonAddress {
private String firstName;
private String lastName;
private String cityName;
private String stateName;
private String countryName;
public PersonAddress(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public PersonAddress(String firstName, String lastName, String cityName) {
this.firstName = firstName;
this.lastName = lastName;
this.cityName = cityName;
}
public PersonAddress(String firstName, String lastName, String cityName, String stateName) {
this.firstName = firstName;
this.lastName = lastName;
this.cityName = cityName;
this.stateName = stateName;
}
public PersonAddress(String firstName, String lastName, String cityName, String stateName, String countryName) {
this.firstName = firstName;
this.lastName = lastName;
this.cityName = cityName;
this.stateName = stateName;
this.countryName = countryName;
}
public static void main(String[] args) {
PersonAddress address = new PersonAddress("David", "Johnson");
System.out.println("First Name::: " + address.firstName + " ::: Last Name::: " + address.lastName);
PersonAddress address1 = new PersonAddress("David", "Johnson", "NewYork");
System.out.println("First Name::: " + address1.firstName + " ::: Last Name::: " + address1.lastName
+ " ::: City Name ::: " + address1.cityName);
PersonAddress address2 = new PersonAddress("David", "Johnson", "NewYork", "NewYork");
System.out.println("First Name::: " + address2.firstName + " ::: Last Name::: " + address2.lastName
+ " ::: City Name ::: " + address2.cityName + " ::: State Name ::: " + address2.stateName);
PersonAddress address3 = new PersonAddress("David", "Johnson", "NewYork", "NewYork", "USA");
System.out.println("First Name::: " + address3.firstName + " ::: Last Name::: " + address3.lastName
+ " ::: City Name ::: " + address3.cityName + " ::: State Name ::: " + address2.stateName
+ " ::: Country Name ::: " + address3.countryName);
}
}
Constructor Chaining
Constructor Chaining is nothing calling one constructor from another constructor is called constructor chaining.
package com.docsconsole.corejava.fpackage;
public class PersonAddress {
private String firstName;
private String lastName;
private String cityName;
private String stateName;
private String countryName;
public PersonAddress(String firstName, String lastName) {
this(firstName, lastName, "Newyork");
System.out.println("Constructor@PersonAddress with two arguments");
this.firstName = firstName;
this.lastName = lastName;
}
public PersonAddress(String firstName, String lastName, String cityName) {
this(firstName, lastName, "Newyork", "Newyork");
System.out.println("Constructor@PersonAddress with three arguments");
this.firstName = firstName;
this.lastName = lastName;
this.cityName = cityName;
}
public PersonAddress(String firstName, String lastName, String cityName, String stateName) {
this(firstName, lastName, "Newyork", "Newyork", "USA");
System.out.println("Constructor@PersonAddress with four arguments");
this.firstName = firstName;
this.lastName = lastName;
this.cityName = cityName;
this.stateName = stateName;
}
public PersonAddress(String firstName, String lastName, String cityName, String stateName, String countryName) {
System.out.println("Constructor@PersonAddress with five arguments");
this.firstName = firstName;
this.lastName = lastName;
this.cityName = cityName;
this.stateName = stateName;
this.countryName = countryName;
}
public static void main(String[] args) {
PersonAddress address = new PersonAddress("David", "Johnson");
System.out.println("First Name::: " + address.firstName + " ::: Last Name::: " + address.lastName
+ " ::: City Name ::: " + address.cityName + " ::: State Name ::: " + address.stateName
+ " ::: Country Name ::: " + address.countryName);
}
}
Private Constructor
Singleton is one of design technique is used for creating only one instance. To avoid creation of the object creation outside of the class we can use the private constructor. Private constructor will not allow you to write the code outside of the class to crate object.
package com.docsconsole.corejava.gpackage;
public class PersonAddress {
private String firstName;
private String lastName;
private PersonAddress(String firstName, String lastName) {
System.out.println("Constructor@PersonAddress with two arguments");
this.firstName = firstName;
this.lastName = lastName;
}
public static void main(String[] args) {
PersonAddress address = new PersonAddress("David", "Johnson");
System.out.println("First Name::: " + address.firstName + " ::: Last Name::: " + address.lastName );
}
}
Super Constructor
Some times we need to initialize the Parent Class variables. Because sometimes Parent Class Variables need to be initialized without instantiating the Object.
package com.docsconsole.corejava.hpackage;
public class Address {
public String cityName;
public String stateName;
public String countryName;
public Address(String cityName, String stateName, String countryName) {
System.out.println("Constructor@Address with Three arguments");
this.cityName = cityName;
this.stateName = stateName;
this.countryName = countryName;
}
}
}
package com.docsconsole.corejava.hpackage;
public class Person extends Address {
public String firstName;
public String lastName;
public Person(String firstName, String lastName) {
super("Newyork", "Newyork", "USA");
System.out.println("Constructor@Person with Two arguments");
this.firstName = firstName;
this.lastName = lastName;
}
}
Copy Constructor
package com.docsconsole.corejava.ipackage;
public class PersonAddress {
public String firstName;
public String lastName;
public PersonAddress(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public PersonAddress(PersonAddress address) {
this.firstName = address.firstName;
this.lastName = address.lastName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}