Re: classes question
- From: Wojtek Bok <wb@xxxxxxxxxx>
- Date: Thu, 27 Apr 2006 14:18:44 GMT
Thomas Weidenfeller wrote:
Typically, the method changes the state as a side effect of doing
something, but it has (unfortunately) also become very popular to
provide methods which just change an object's state, but doing nothing else.
This statement with:
In Java, methods just changing an object's internal variable/state are
called setters. Methods just reporting an object's state/variable are
called getters.
confuses me.
Getters and setters are there to change the state. Sometimes they also do some ancillary processing, but mostly they change the values of private variables.
So in the first statement you label this as a bad thing, while in the second statement you endorse it. So which is it?
Not trying to start a flame-war here. Just confused.
I exclusively use getters/setters for all access to object variables (state change). This has saved me where I needed a setter to also change another state variable. A trivial example which assumes that the setting of the name is done once, whereas retrieving the full name happens many times:
Original:
public void setGivenName(String name)
{
ivGivenName = name;
}
Changed:
public void setGivenName(String name)
{
ivGivenName = name;
ivFullName = ivGivenName + " " + ivSurname;
}
.
- Follow-Ups:
- Re: classes question
- From: Mark Thomas
- Re: classes question
- From: Thomas Weidenfeller
- Re: classes question
- References:
- classes question
- From: linuxnooby
- Re: classes question
- From: Thomas Weidenfeller
- classes question
- Prev by Date: Re: Help needed making a move into a Apple iBook
- Next by Date: Re: classes question
- Previous by thread: Re: classes question
- Next by thread: Re: classes question
- Index(es):
Relevant Pages
|