How to make a constructor that takes 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 their instance variables.

If I had certain data that only had firstName and lastName, no middleInitial, how could I make the constructor so that instead of three? instead of <3>

+4
source share
9 answers

You just 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) { ... } 

Then callers can use the appropriate constructor depending on their needs.

+10
source

Well, two options:

  • Just create a constructor with three parameters and call it using null or an empty string for middleInitial
  • Overload the constructors, perhaps by calling them from one another.

As an example, the last one uses an empty string as the initial default average:

 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 are calling from the call site. So you can do:

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

or

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

... but you cannot do:

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

and expect the compiler to decide to use the two-name option.

+7
source

In Java, constructors cannot have default arguments. Your only option is to write two constructors. Fortunately, Java allows you to call constructors from other constructors. You can 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); } ... } 
+2
source

You can 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; } 
0
source

Define 2 constructors, one with two parameters and one with three parameters.

0
source

You can write two constructors.

 public Person( String firstName, String lastName, String middleName ) { ... } public Person( String firstName, String lastName ) { ... } 
0
source
 public void myBike(String name, String color) { System.out.println("My bike name is " + name + " and is " + color + "."); } public void myBike(String name, String color, float height) { System.out.println("My bike name is " + name + " and is " + color + "."); System.out.println("My bike is also " + height + " inches tall."); } 
0
source
 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; } } 
0
source

Figure Builder ...

 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); } } } 
0
source

All Articles