public class Persona {
int Codigo;
String Nombre;
public Persona(int Codigo, String Nombre){
this.Codigo = Codigo;
this.Nombre = Nombre;
}
public void setCodigo(int Codigo){
this.Codigo = Codigo;
}
public int getCodigo(){
return this.Codigo;
}
public void setNombre(String Nombre){
this.Nombre = Nombre;
}
public String getNombre(){
return this.Nombre;
}
}
Or is there a much shorter (realiable) way to do it?
It depends what you mean by “do it”. Getters and setters are better than public fields – but do you really need them in the first place?
Just to “correct” your current design, assuming you do want getters and setters, I would change it to:
The main changes are in terms of capitalization of fields, and making the fields private. Personally I also don’t use “this.” where I don’t have to. I also have a space between () and {. Those are more personal preferences than anything else.
Finally, I’ve made the class
final– I’m a believer in “design for inheritance or prohibit it” – if you do need to derive from this class, consider what kind of specialization you want to support.