Hi, when I started to look into programming, one of the things I remember many programmers are quite keen on are setters and getters. I totally understand the importance of them but there is one thing that isn't really clear to me. If I have a class with some private members declared in it and setters and getters like:
public class MyTest{
private int variable1;
private int variable2;
...
//setter1
public void setVar1( int theFirst){
variable1 = theFirst;
}
//setter2
public void setVar2( int theSecond){
variable2 = theSecond;
}
//getter1
public int getVar1(){
return variable1;
}
//getter2
public int getVar2(){
return variable2;
}
public void doSomething( int theFirst, int theSecond){
...
}
}
and a test class like
public class TheTest{
int firstVar = 34;
int secondVar = 20;
MyTest probe = new MyTest();
setVar1(firstVar);
setVar2(secondVar);
doSomething( firstVar, secondVar);
}
Now my question is this: in the above code I call the setter once to set the value of
the instance variables. I need to use these variables (variable1 and variable2) in my doSomething function but
I am thinking, because I have the setters, this type of call doSomething( firstVar, secondVar);
- in which I
pass the local variables to the function - is needless: I might as well do instead doSomething();
, passing no parameters at all and then inside doSomething edit directly variable1 and variable2.
So what's the best way? using setters or passing variables to the function?
thanks