1

I tried to apply the this() method for calling the same constructor in java for trying to make a constructor wehere just vy providing the name and the class it would select the parameters of the character but its not working, im new to using this in java.

type public class Jugador{
    //campos
    private String nombre;
    private String clase;
    private int vida;
    private int ad;
    private int ap;
    private int def;
    private int mr;

    //constructores
    private Jugador(String nombre, String clase, int vida, int ad, int ap, int def, int mr){
        this.nombre = nombre;
        if (clase.equals("hunter") || clase.equals("warrior") || clase.equals("assasin") || clase.equals("tank")){
            this.clase = clase;
        }
        else{
            this.clase = "normal";
        }
        this.vida = vida;
        this.ad = ad;
        this.ap = ap;
        this.def = def;
        this.mr = mr;
    }

    public Jugador(String nombre, String clase){
        if (clase.equals("hunter")){
            this(nombre, clase, 120, 50, 0, 40, 0);
        }
        else if (clase.equals("warrior")){
            this(nombre, clase, 100, 70, 0, 80, 0);
        }
        else if (clase.equals("assasin")){
            this(nombre, clase, 60, 120, 0, 20, 0);
        }
        else if (clase.equals("tank")){
            this(nombre, clase, 200, 20, 0, 80, 0);
        }
        else if(clase.equals("normal")){
            this(nombre, clase, 80, 80, 0, 80, 0);
        }
    }


    }here
  • 2
    Please post the error message. That is going to be necessary in order for us to make sure that we are looking at the same error. – davidalayachew Aug 06 '23 at 04:40
  • 2
    When using `this()`, it must be the first statement, so you can't use an `if` statement and then `this()`. – markspace Aug 06 '23 at 04:56

2 Answers2

2

You cannot use this() (or super()) inside an if statement. Calls to this() (or super()) has to to be the first statement in a constructor.

See Why do this() and super() have to be the first statement in a constructor? post for why that is the case. 1

There are several ways you can fix this. You can decide not to have a public constructor for the class and instead enable creating instance of the class via a public static method.

public static class Jugador {
    //campos
    private String nombre;
    private String clase;
    private int vida;
    private int ad;
    private int ap;
    private int def;
    private int mr;

    private Jugador(String nombre, String clase, int vida, int ad, int ap, int def, int mr) {
        this.nombre = nombre;
        if (clase.equals("hunter") || clase.equals("warrior") || clase.equals("assasin") || clase.equals("tank")) {
            this.clase = clase;
        } else {
            this.clase = "normal";
        }
        this.vida = vida;
        this.ad = ad;
        this.ap = ap;
        this.def = def;
        this.mr = mr;
    }

    public static Jugador buildJugador(String nombre, String clase) {
        if (clase.equals("hunter")) {
            return new Jugador(nombre, clase, 120, 50, 0, 40, 0);
        } else if (clase.equals("warrior")) {
            return new Jugador(nombre, clase, 100, 70, 0, 80, 0);
        } else if (clase.equals("assasin")) {
            return new Jugador(nombre, clase, 60, 120, 0, 20, 0);
        } else if (clase.equals("tank")) {
            return new Jugador(nombre, clase, 200, 20, 0, 80, 0);
        } else if (clase.equals("normal")) {
            return new Jugador(nombre, clase, 80, 80, 0, 80, 0);
        } else {
            throw new RuntimeException("Invalid value passed for clase");
        }
    }
}

Then the callers cannot directly invoke the constructor (as it is private), but use the buildJugador method as,

Jugador jugador = Jugador.buildJugador("some-value", "hunter");

1 That might change in the future with JEP 447: Statements before super(). But it is not planned to be part of any future Java release as of now.

Thiyagu
  • 17,362
  • 5
  • 42
  • 79
  • JEP 447 is interesting, but according to the issue tracker, there is no target release for the preview yet. IMO, the preview is highly unlikely to land *before* Java 22. – Stephen C Aug 06 '23 at 06:17
  • @StephenC Yes. It may not even make it at all (be dropped for some reason). That is why I've said *it might* change in the future depending on how the JEP goes – Thiyagu Aug 06 '23 at 06:18
  • Agreed. Just filling in the dotted lines for readers who are overly eager and/or who don't understand how innovations happen in Java. – Stephen C Aug 06 '23 at 06:28
  • @StephenC I've rephrased it to be more clear – Thiyagu Aug 06 '23 at 06:32
  • @user16320675 The JEP title has the word *preview*. We could say the JEP is in review. Anyway I've removed the word *preview* to avoid ambiguity. – Thiyagu Aug 06 '23 at 08:03
0

"... a constructor wehere just vy providing the name and the class it would select the parameters of the character ..."

So, instead of utilizing a private constructor,

private Jugador(String nombre, String clase, int vida, int ad, int ap, int def, int mr)

Use a private method to add the values, vida, ad, etc.

private void values(int vida, int ad, int ap, int def, int mr) {
    this.vida = vida;
    this.ad = ad;
    this.ap = ap;
    this.def = def;
    this.mr = mr;
}

Then, from your public constructor, just apply values accordingly.

I merged your code from the private constructor here.

public Jugador(String nombre, String clase){
    this.nombre = nombre;
    this.clase = switch (clase) {
        case "hunter", "warrior", "assasin", "tank" -> clase;
        default -> "normal";
    };
    switch (clase) {
        case "hunter" -> values(120, 50, 0, 40, 0);
        case "warrior" -> values(100, 70, 0, 80, 0);
        case "assasin" -> values(60, 120, 0, 20, 0);
        case "tank" -> values(200, 20, 0, 80, 0);
        case "normal" -> values(80, 80, 0, 80, 0);
    }
}
Reilas
  • 3,297
  • 2
  • 4
  • 17