Rich Maes
Rich Maes

Reputation: 1222

Constructors calling other constructors gives an error

This seems like this should just work. This is being compiled as part of a JavaFX application. I have several constructor classes with different arguments, but effectively they are very similar to one another.

The first is a String, String, String constructor and the second is a String, String, String, int. I wanted to just write the String, String, String, and then make a minor adjustment to the String, String, String, int variant and call the original constructor. I get an error that says:

error: cannot find symbol
this.DataLineRegister(new_register_name,new_register_model,new_register_default);
   symbol: method DataLineRegister(String,String,String)
1 error

Here is the code. I have tried with and without the this reference. Also, tried self, but I guess that might only be a C thing.

public DataLineRegister (String new_register_name, String new_register_model, String new_register_default) {
  register_name = new_register_name;
  register_model = new_register_model;

  try {
     register_default = new BigInteger(new_register_default,16);
  } catch (NumberFormatException e) {
     System.out.println("Input is not a hexadecimal number");
  }
}

public DataLineRegister (String new_register_name, String new_register_model, String new_register_default, int new_bit_width) {
    defaultRegisterBitWidth = new_bit_width;
    this.DataLineRegister(new_register_name, new_register_model, new_register_default);
}

What am I doing wrong here?

Upvotes: 0

Views: 51

Answers (1)

Bathsheba
Bathsheba

Reputation: 234715

You need to write this(new_register_name,new_register_model,new_register_default); instead. That is the correct syntax for calling a delegated constructor.

Also, note that it has to be the first statement in the constructor body.

Upvotes: 2

Related Questions