Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have two constructors with same number of arguments but for different variables in java [closed]

In my case, The class will have two constructors both taking 3 strings as arguments but one of the string variables that is to be initialized in one of the constructors may differ. Is it possible to implement the following:

class A {
   String x = null;
   String y = null;
   String z = null;
   String a = null;

   A(String x, String y, String z) {
      ....
   }

   A(String a, String y, String z) {
      ....
   }
}
like image 208
fmjaguar3 Avatar asked Jun 17 '13 22:06

fmjaguar3


People also ask

Can two constructors have the same number of parameters?

The technique of having two (or more) constructors in a class is known as constructor overloading. A class can have multiple constructors that differ in the number and/or type of their parameters. It's not, however, possible to have two constructors with the exact same parameters.

Can we write two constructors that have the same number and type of arguments for the same class?

You cannot write two constructors that have the same number and type of arguments for the same class, because the platform would not be able to tell them apart. Doing so causes a compile-time error.

Can I have 2 constructors in Java?

Constructor Overloading - Multiple Constructors for a Java Class. A class can have multiple constructors, as long as their signature (the parameters they take) are not the same. You can define as many constructors as you need.

Can I have 2 constructors?

A class can have multiple constructors that assign the fields in different ways. Sometimes it's beneficial to specify every aspect of an object's data by assigning parameters to the fields, but other times it might be appropriate to define only one or a few.

Can multiple constructors have the same name?

Constructor Overloading in C++ In C++, We can have more than one constructor in a class with same name, as long as each has a different list of arguments. This concept is known as Constructor Overloading and is quite similar to function overloading.

How many constructors one class can have what are the differences between them?

You can have 65535 constructors in a class(According to Oracle docs). But IMPORTANTLY keep this in your mind. We achieve this only by CONSTRUCTOR OVERLOADING ( https://beginnersbook.com/2013/05/constructor-overloading/ ). You can create many constructors but with different signatures.

How many arguments can be passed to a constructor?

This method takes four arguments: the loan amount, the interest rate, the future value and the number of periods.

How I can call multiple constructors by making a single object?

First, I will pass default constructor for class A that will automatically be called while our object is created. Then I will make another constructor having some parameters, [you may pass any parameter] In the second, third, and fourth constructor I will pass int parameters.


2 Answers

No, but a quick solution is to use static helpers:

class A {

  String x, y, z, a;

  /** Constructor. Protected. See static helpers for object creation */
  protected A(String x, String y, String z, String a) {
    this.x = x;
    this.y = y;
    this.z = z;
    this.a = a;
  }

  /** Construct a new A with an x, y, and z */
  public static A fromXYZ(String x, String y, String z) {
    return new A(x, y, z, null);
  }

  /** Construct a new A with an a, y, and z */
  public static A fromAYZ(String a, String y, String z) {
    return new A(a, null, y, z);
  }
}
like image 100
Andy Avatar answered Sep 22 '22 11:09

Andy


Is it possible to implement the following

No.

Because the compiler has no built-in crystal ball in order to choose the appropriate constructor at compile time.

Please be aware of two points:

  • The parameter names in the constructor signature are lost after compilation - they are pure human sugar. So they cannot be use to dispatch between both ctors.

  • The parameter names have nothing to do with the field names. That both are usually the same is of no concern to the compiler.

like image 44
A.H. Avatar answered Sep 20 '22 11:09

A.H.