• Post Reply Bookmark Topic Watch Topic
  • New Topic

getters and setters  RSS feed

 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys,

watched a few youtube videos on getters and setters...i just dont get it
i didnt like the way most are explaining it

has anybody got a good tutorial clearly explaining the practical usage of these methods

is this how/where they would be used:

i have a horse class with 3 instance variables
speed stamina energy

do getters and setters let me create an object with these variables always set to a specific value?

any help appreciated

is there some notes on this in the oracle pages? couldnt find any and my java book doesnt have anything on getters and setters only found accessors used a few times but didnt explain it does code ranch have a tutorial?

thanks
 
Campbell Ritchie
Marshal
Posts: 55786
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jon ninpoja wrote:. . . do getters and setters let me create an object with these variables always set to a specific value?  . . .
No.

Mutator methods (the proper name for setters) and accessor methods (the proper name for getters) are part of the Bean pattern. That means you have public get and set methods for each field and a public no‑arguments constructor. The state of the object can be written to an XML file and it can later be restored exactly the same from that XML file. There is another discussion here. If you are not using the Bean pattern, then get methods may allow outside code to alter the state of your object and set methods most probably will. Remember that such methods may expose the implementation of your object to the outside world. This is particularly serious if any fields are mutable reference types, because they need a defensive copy before they are let into our object or out of it.
 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi campbell,

i read the articles you posted,though i must admit i didnt really get all of it.

i also found an article saying:
Getters and setters are often criticized as being not proper OO. On the other hand most OO code I've seen has extensive getters and setters.

When are getters and setters justified? Do you try to avoid using them? Are they overused in general?

so,is there a better way to accomplish the same thing getters and setters do? or is it unavoidable,and you will have to use them at some point
or should they be avoided as much as possible?

where should i be looking?

thanks...
 
Stephan van Hulst
Saloon Keeper
Posts: 7820
142
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Getters and setters simply are tools. There's nothing inherently bad or non-OO about them.

The problem is that most people tend to overuse them. From a procedural programming perspective, most people are used to getting information out of a record, and then performing a calculation based on that information:

The problem is that we're not really using the power of object oriented programming here. If we're only interested in the distance between two points, then why are we asking about the x and the y coordinates? If we are only ever interested in the distance, we can get rid of the x and y getters, and just add a distance property:

Having getX() and getY() is not necessarily bad. It's just bad if you use them to perform calculations outside of the Position class, that could have been done inside the Position class.
 
Carey Brown
Bartender
Posts: 3011
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:

Stephan, Why are you using "0L" instead of
 
Stephan van Hulst
Saloon Keeper
Posts: 7820
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some code formatters will insert a space after the cast operator, and for some reason, many people interpret
as

I have found that adding to 0L leads to less confusion regarding operator precedence, and is easier to read than

which again, may be rewritten by a code formatter.
 
Campbell Ritchie
Marshal
Posts: 55786
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:. . .
The problem is that we're not really using the power of object oriented programming here. If we're only interested in the distance between two points, then why are we asking about the x and the y coordinates? If we are only ever interested in the distance, we can get rid of the x and y getters, and just add a distance property:
Agree: isn't that what OO is all about. Let the train object take the strain.

. . .
Surely you would use this method rather than sqrt? It promises not to overflow, but that probably doesn't apply to the arguments, where there is a risk of integer overflow.
 
Stephan van Hulst
Saloon Keeper
Posts: 7820
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I'm not as familiar with the Math methods as I once used to be. Updated:
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66207
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's not drift from the subject.

Be aware that the bean pattern, to include getters and setters, is required in many scenarios. For example, when consuming bean data in a JSP, or populating an entity object in ORMs (like JPA or Hibernate).
 
Junilu Lacar
Sheriff
Posts: 11166
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jon ninpoja wrote:
Getters and setters are often criticized as being not proper OO. On the other hand most OO code I've seen has extensive getters and setters.

You should recognize that not all Java code is OO code. I don't see OO-ness as a binary quality either. As with most other things, adhering to OO principles can be done too much or too little, with little to no effect in some cases and disastrous consequences in others.

The danger with getters and setters is that they can break encapsulation. Determining whether they actually do, however, has to be on a case-to-case basis and should be weighed against any potential risks involved. The most common issues you'll have to consider are related to unexpected or unwanted side-effects that can happen because encapsulation was broken. These can lead to bugs and issues related to concurrency, and these could compromise your program's correctness and reliability. Techniques like "make defensive copies" can mitigate risks but sometimes, as Stephan's example showed, eliminating them altogether is also a valid option. Keep in mind what Bear pointed out though about getters/setters being a part of the bean pattern. Again, always consider context.
 
rick pine
Ranch Hand
Posts: 90
Java Netbeans IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to share my experience, i recently went through getters and setters in class.
The way i learned it is by using the not so common terms, accessor and mutator.
I think of it like this;  if i have a field and i want to make a change to it, i will mutate the value of said field so i code an mutator.

public void int setSomething( int something){

}

and then if i need to access that value from a different class, i code an accessor, which only allows me to read , but not modify the value created by the mutator.

public int getSonething(){

return somethig;

}

i dont know if this helps, im new at this  but that is how i learned the concept of getter and setter.
Someone else already mentioned this, but your code does not need getters and setters.
 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks rick and all!!!
 
Campbell Ritchie
Marshal
Posts: 55786
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:. . . the bean pattern, to include getters and setters, is required in many scenarios. . . .
And in these scenarios we would have to trust that people using the objects would be careful not to break encapsulation?
 
Piet Souris
Rancher
Posts: 1984
67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The example given on distance only shows the usefulness of having getters. A distance function is one that has some requirements (like d(x,x) >= 0 d(x,y) = d(y,x) et cetera), and that is normally something you would leave to a Distance class, having a BiFunction as one of its member, Getters come in handy then.
 
Frits Walraven
Creator of Enthuware JWS+ V6
Saloon Keeper
Posts: 2987
223
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Congratulations Jon Ninpoja !!

This question has been selected for the September Journal

Cow!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!