1. Home
  2. Computing & Technology
  3. Java

The Constructor Method

By , About.com Guide

More Than One Constructor Method

When designing your object classes you are not limited to only using one constructor class. You might decide there are a couple of ways an object can be initialized. The only constraint on using more than one constructor method is that they must differ in the parameters they accept.

Imagine that at the time we create the Person object we might not know the username we want to give to a person. Let's add a new constructor method that sets the state of the Person object using only the firstname, lastname and address:

public class Person {

  private String firstName;
  private String lastName;
  private String address;
  private String username;

  // The constructor method
  public Person(String firstName, String lastName, String address, String username)
  {
    this.firstName = firstName;
    this.lastName = lastName;
    this.address = address;
    this.username = username;
  }

  // The new constructor method
  public Person(String firstName, String lastName, String address)
  {
    this.firstName = firstName;
    this.lastName = lastName;
    this.address = address;
    this.username = "";
  }

  // A method to display the state of the object to the screen
  public void displayPersonDetails()
  {
    System.out.println("Name: " + firstName + " " + lastName);
    System.out.println("Address: " + address);
    System.out.println("Username: " + username);
  }
}

Note that the second constructor method is also called "Person" and it also does not return a value. The only difference between it and the first constructor method is the parameters – this time it is only expecting three string values: firstName, lastName and address.

We can now create Person objects in two different ways:

public class PersonExample {

  public static void main(String[] args) {

    Person dave = new Person("Dave", "Davidson", "12 Pall Mall", "DDavidson");
    Person jim = new Person("Jim","Davidson", "15 Kings Road");
    dave.displayPersonDetails();
    jim.displayPersonDetails();
  }

}

dave will be created with a state of: firstname = "Dave", lastname = "Davidson", address = "12 Pall Mall", username = "DDavidson". Jim will not get a username and instead will have a state of: firstname = "Jim", lastname = "Davidson", address = "15 Kings Road", username = "".

A Quick Recap

Constructor methods are only called when a new instance of an object is created. They:

  • must have the same name as the class
  • do not return a value
  • can have none, one, or many parameters
  • can number more than one as long as each constructor method has a different set of parameters
  • can have parameter names the same as the private fields as long as the "this" keyword is used
  • are called using the "new" keyword
Explore Java
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Java
  4. Java Syntax
  5. Working With Objects
  6. The Constructor Method>

©2009 About.com, a part of The New York Times Company.

All rights reserved.