G
glunk
I went to a Java class (training!) a while ago. The instructor was actually
a real programmer. I am used to Microsoft training in which the person doing
the training does not actually ever DO the work. Anyway, the instructor told
us it was a good idea to either set the values of instance variables by
passing them into the constructor OR by setter methods, but not both. One
class I am working on which was created by someone else has the values
passed into the constructor (with no zero parameter constructor) AND setter
methods. We were going to do this kind of thing
public MyClass (String lastName, String firstName) {
setLastName(lastName);
setFirstName(firstName);
}
to ensure that any setter code which may do something to the value is used.
But then, if you can see my other post entitled "getting used to Java -
question about 'style'" this seems to be calling a nonfinal method (the
setters) from within a constructor.
So far, in our project, we do not use the setters. I cannot think of why you
would want to have only one constructor with the params for the instance
variables AND the setter methods if you cannot use the setter methods within
the contructor.
Can someone comment on whether or not it is generally wise to not have
setters if the instance vars have to be set in the constructor? In this
example, I cannot even think of a value changing.
Thanks
S
a real programmer. I am used to Microsoft training in which the person doing
the training does not actually ever DO the work. Anyway, the instructor told
us it was a good idea to either set the values of instance variables by
passing them into the constructor OR by setter methods, but not both. One
class I am working on which was created by someone else has the values
passed into the constructor (with no zero parameter constructor) AND setter
methods. We were going to do this kind of thing
public MyClass (String lastName, String firstName) {
setLastName(lastName);
setFirstName(firstName);
}
to ensure that any setter code which may do something to the value is used.
But then, if you can see my other post entitled "getting used to Java -
question about 'style'" this seems to be calling a nonfinal method (the
setters) from within a constructor.
So far, in our project, we do not use the setters. I cannot think of why you
would want to have only one constructor with the params for the instance
variables AND the setter methods if you cannot use the setter methods within
the contructor.
Can someone comment on whether or not it is generally wise to not have
setters if the instance vars have to be set in the constructor? In this
example, I cannot even think of a value changing.
Thanks
S