File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes getter/setter methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "getter/setter methods" Watch "getter/setter methods" New topic

getter/setter methods

Don Jennings

Joined: Aug 08, 2003
Posts: 3
Hi, again Jacquie. In a separate topic on inheritance, you wrote
If a parent class has a private data member but public get/set methods for that member, then the subclass can use the inherited get/set methods to access its own private attributes
and then followed that with a code sample. Recently, I read an interesting article from Allen Holub called Why getter and setter methods are evil in which he claims that too many of these impact a systems maintainability by exposing implementation details.
So (and here's the question, finally) what role do you think get/set methods should have in object oriented systems?
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

I agree with that article to the extent that yes, some newbie programmers have a tendency to add getters and setters for every member variable, and this is bad. You shouldn't have getters and setters for every member; indeed, getters and setters don't even need to correspond to individual member variables: they correspond to observable properties of the object, which might be represented by more than one variable internally, or might be computed from other state.
But his essential thesis in that article is that an object having any observable properties at all is just bad design -- and that's just hyperbole to sell newspapers. Use getters and setters when you need to, but don't if you don't.
If you've got a Person object, Alan's argument says that it can't have a getAge() method, only a displayAge(DisplaySubstrate) method. That's just silly. Sells papers, though.
[ October 17, 2003: Message edited by: Ernest Friedman-Hill ]

[Jess in Action][AskingGoodQuestions]
Eric Sexton
Ranch Hand

Joined: Sep 12, 2003
Posts: 133
I totally agree with Ernest.
It's a bit irresponsible to call "getters and setters evil". Especially when he says later:
"Design, by nature, is a series of trade-offs. Every choice has a good and bad side, and you make your choice in the context of overall criteria defined by necessity. Good and bad are not absolutes, however. A good decision in one context might be bad in another."
I think the last quote is the most important one to remember from the article.
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
A good heuristic from :

Very short summary: It is okay to use accessors to get the state of an object, as long as you don't use the result to make decisions outside the object. Any decisions based upon the state of an object, should be made inside the object itself.

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
I agree. Here's the link:
subject: getter/setter methods
It's not a secret anymore!