×
DocsConsole

DocsConsole

Java Constructor Tutorial

By | | Updated : 2019-10-28 | Viewed : 198 times

Constructors In Java

Constructors in Java allows us to initialize Object with required values when we want override the default values. After creation of Object, the default values will be initialized. To override these values, we can use the Constructors.

Constructors In Java

Java Constructors looks like method. Here Java distinguish the Constructor with Method by verifying the Method return type. Constructor does not have return type whereas Method have the return type. Let’s try to understand the constructor in Java below given example. Mostly, Constructor is not necessary as Class has Setter methods to initialize the Object Properties.

PersonAddress.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 public PersonAddress() is Constructor which does not have return type. But public String personAddress() is Method which has return type.

Types Of Constructors

Based on the Number of Arguments passed to the constructor, Constructors are categorized as three types.

    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 Constructor in Class then Java will provide one Default Constructor. After Creation of Java object, Default Constructor will be executed. Let’s try to understand same context mentioned above with below given example.

Default Constructor Example: PersonAddress.java
package com.docsconsole.corejava.bpackage;


	public class PersonAddress {
		
	public static void main(String[] args) {
		PersonAddress address = new PersonAddress();
	}
}

Note: Java compiler will replace with Default Constructor in the Class wherein no Constructor is existed.

No-Args Constructor

When you implement Constructor with no arguments in Java then those type of constructor is called No-Args (No Argument) Constructor. Please try with below given to understand No-Args Constructor.

No Argument Constructor Example: PersonAddress.java
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., public PersonAddress() will be called and executed after initialization.

Parameterized Constructor

Constructor with arguments is called as Parameterized Constructor. Here Parameterized Constructor can be overloaded. We will discuss this in the next section. Please find the example for Parameterized Constructor given below.

Parameterized Constructor Example: PersonAddress.java
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 Constructor Overloading is also been provided in Java. When you implement more than one constructor in Class then constructor is said to be overloaded. Please see the below given example.

Constructor Overloading: PersonAddress.java
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.

Constructor Chaining in Java with Example
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.

Private Constructor Example
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.

Super Constructor Example
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;
	}

}
}
Super Constructor Example
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

Copy Constructor is one of the ways to initialize the Complex object. Here we will make Deep Copy of the Complex Object. Let’s try with example to understand the Copy Constructor.

Copy Constructor Example: PersonAddress.java
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;
	}

}
Leave A Reply