iggy2012
iggy2012

Reputation: 41

How to make constructor that takes in 2 if there are 2 parameters, or 3 if there are 3

New to Java...

I have a name class that has:

private String firstName;
private String middleInitial;
private String lastName;

as its instance variables.

If I had certain data that had only firstName and lastName, no middleInitial, how would I make the constructor so that it took only 2 parameters instead of three?

Upvotes: 4

Views: 29468

Answers (9)

Martin Pfeffer
Martin Pfeffer

Reputation: 12627

Builder pattern...

class Name {

            Builder builder;


            public String getSurname() {
                return builder.surname;
            }

            // getter...


            public Name(Builder builder) {
                this.builder = builder;
            }


            class Builder {

                String surname = "";
                String middleName = "";
                String name = "";


                Builder surname(String surname) {
                    this.surname = surname;
                    return this;
                }


                Builder middleName(String middleName) {
                    this.middleName = middleName;
                    return this;
                }


                Builder name(String name) {
                    this.name = name;
                    return this;
                }


                Name build() {
                    return new Name(this);
                }

            }
        }

Upvotes: 0

OpenSource
OpenSource

Reputation: 2257

public Class Name{
private String first;
private String middle;
private String last;

public Name(String first, String middle, String last){
this.first = first;
this.middle = middle;
this.last = last;
}

public Name(String first, String last){
this.first = first;
this.last = last;
}

}

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1502756

Well, two options:

  • Just have a constructor with three parameters, and call it using null or the empty string for middleInitial
  • Overload the constructors, possibly calling one from the other.

As an example for the latter, using an empty string as the default middle initial:

public Person(String firstName, String middleInitial, String lastName)
{
    this.firstName = firstName;
    this.middleInitial = middleInitial;
    this.lastName = lastName;
}


public Person(String firstName, String lastName)
{
    this(firstName, "", lastName);
}

However, the compiler will need to know which one you're calling from the call site. So you can do:

new Person("Jon", "L", "Skeet");

or

new Person("Jon", "Skeet");

... but you can't do:

// Invalid
new Person(firstName, gotMiddleInitial ? middleInitial : ???, lastName);

and expect the compiler to decide to use the "two name" variant instead.

Upvotes: 9

jpm
jpm

Reputation: 3155

In Java, constructors can't have default arguments. Your only option here is to write two constructors. Fortunately, Java does allow you to call constructors from other constructors. You could do something like:

public class MyClass {
    private String firstName;
    private String middleInitial;
    private String lastName;

    public MyClass(String firstName, String middleInitial, String lastName) {
        this.firstName = firstName;
        this.middleInitial = middleInitial;
        this.lastName = lastName;
    }

    public MyClass(String firstName, String lastName) {
        this(firstName, "", lastName);
    }

    ...
}

Upvotes: 2

Dennis
Dennis

Reputation: 4017

public void myBike(String name, String color)
{
      System.out.println("My bike's name is " + name + " and is " + color + ".");
}

public void myBike(String name, String color, float height)
{
      System.out.println("My bike's name is " + name + " and is " + color + ".");
      System.out.println("My bike is also " + height + " inches tall.");
}

Upvotes: 0

Mike Caputo
Mike Caputo

Reputation: 1156

You can write two constructors.

public Person( String firstName, String lastName, String middleName ) { ... }

public Person( String firstName, String lastName ) { ... }

Upvotes: 0

MRAB
MRAB

Reputation: 20664

Define 2 constructors, one with 2 parameters and one with 3 parameters.

Upvotes: 0

Hunter McMillen
Hunter McMillen

Reputation: 61540

You could use two constructors:

public Person(String firstName, String lastName)
{
    this(firstName, null, lastName);
}

public Person(String firstName, String middleInitial, String lastName)
{
    this.firstName     = firstName;
    this.middleInitial = middleInitial;
    this.lastName =    = lastName;
}

Upvotes: 0

matt b
matt b

Reputation: 140011

You simply write a constructor with two parameters and a constructor with three

public YourClass(String firstName, String lastName) {
   ...
}

public YourClass(String firstName, String middleInitial, String lastName) {
    ...
}

Callers can then choose to use the appropriate constructor based on their needs.

Upvotes: 10

Related Questions