vendredi 6 mars 2015

Should I declare variables in the class, if the class has only a constructor?


Vote count:

0




Let's say I have a class:



public class Foo {
private int x;
private int y;
public Foo(int x, int y) {
this.x = x;
this.y = y;
/**
* here comes a lot of code within this constructor
* that uses these x and y variables.
*/
}
}


I understand, that if I had some other methods in this class that would use the x and y variables, I should have copied the constructor's arguments into the fields like I did above, but what if I don't have any other methods but only the constructor? Should I still create the fields or my class should look like this:



public class Foo {
public Foo(int x, int y) {
/**
* here comes a lot of code within this constructor
* that uses these x and y variables.
*/
}
}


What is the right style?



asked 41 secs ago







Should I declare variables in the class, if the class has only a constructor?

Aucun commentaire:

Enregistrer un commentaire