Get / Install from C # to Java

I am working on a project to translate a C # project to Java. I have the following Get / Set block in C #

 public Unit[] Units
{
    get
    {
        Unit[] units_aux = new Unit[this.list_units.Count];
        this.list_units.CopyTo(units_aux);
        return units_aux;
    }
    set
    {
        if (value == null) return;
        Unit[] units_aux = (Unit[])value;
        this.list_units.Clear();
        foreach (Unit u in units_aux)
            this.lista_units.Add(u);
    }
}

      

I want to translate this to Java, but I was unable to translate it without syntax errors. I'm very new to Java, so maybe this is the main question, but I haven't found any information on how to do this, which won't lead to errors.

thanks for the help

+3


source to share


1 answer


You basically need to convert it into a couple of methods:

public Unit[] getUnits() {
    // Method body
}

public void setUnits(Unit[] value) {
    // Method body
}

      

Java has no language-level properties - above it is basically just a (very common) convention.



I should note, by the way, that this C # code is really not very nice:

  • There are easier ways to convert an array to a list and vice versa
  • Setter ignores null when I expect it to throw an exception
  • When cloning an array, it does not have the normally expected behavior (at least my expectations) if you set a property and then change the contents of the array. It is generally a bad idea to have an array as a property type; if you could get away with creating a read-only collection it would be better and make life a lot easier.
+11


source







All Articles