File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Write a getter and setter to each private field Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Write a getter and setter to each private field" Watch "Write a getter and setter to each private field" New topic
Author

Write a getter and setter to each private field

Leon Chen
Ranch Hand

Joined: Apr 09, 2003
Posts: 96
1


Leon - SCJP, SCWCD, SCBCD, ICED
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1282

Hi Leon,

no it doesn't make sense!

If you make all members private and make them in general all accessible through simple getters and setters it doesn't gain you anything but more work to write getters and setters instead of simply making all fields public

You should not propagate more details through the public API of a class than is necessary for the clients of this class to properly use it. If you have some fields/instance variables which are used only for internal purposes then it doesn't make sense to make them accessible via getters or setters to the outside world. Moreover it won't make sense to have getters as well as setters for all fields. Sometimes either getters or setters will be enough etc.

As a general advice you should think more carefully about the design of your classes and applications and not just follow conventions like the JavaBean standard blindly by creating getters and setters which aren't needed

Marco
Leon Chen
Ranch Hand

Joined: Apr 09, 2003
Posts: 96
2
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1282

Leon, I hope you don't get me wrong here...

I didn't want to say that you shouldn't use getters/setters at all. IF you need access to some member variables of a class you should of course use accessor methods like this article proposes for the reasons mentioned there. BUT you shouldn't blindly provide accessors for all member variables if they aren't really needed.

Another thing is that lots of tools for different purposes require classes to conform to the JavaBean standard so you probably may have no choice other than to add getters/setters for all members. Besides this link is some years old and opinions about these getters and setters everywhere in Java may have changed.

Of course this is my opinion and if you're company requires you to conform to this standard you won't have a choice anyway... Still I would say it's not very good design to add all those methods just because some tools or standards require them. Instead decide individually from case to case where and if you need accessors!

Marco
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

I would argue vigorously against such a standard; it's essentially the same as a standard mandating no private instance variables at all, which I'm sure you'll agree is a bad idea.

Use accessors when you need to give other classes access to a variable. Use none when you don't. Always make instance variables private.


[Jess in Action][AskingGoodQuestions]
Leon Chen
Ranch Hand

Joined: Apr 09, 2003
Posts: 96
3
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19719
    
  20

Private getters and setters make no sense at all, unless the setter performs some checks. If not, it's just an extra method call.

For any other accessor, I'd only use getters and setters if:
a) you want to really publish the variable
b) you want to allow subclasses to override the method to return something else / set something else


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Leon Chen
Ranch Hand

Joined: Apr 09, 2003
Posts: 96
4
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1282

Hi Leon,

I'd say the knowledge of some of your colleagues is a little bit dated

The idea to make private getters/setters really makes no sense. The only reason someone could choose to create accessors for all members is, that you it's easier to make them public later. This idea is just stupid.
Most of the time you should pretty well know if a member variable has to be publicly available at the time you design and write the code and just adding accessors because one day someone maybe could need them is bullshit.
On the other hand with any modern IDE it needs just a few clicks into the refactoring menu to automatically create getters and/or setters for any member variable AND you can usually even choose if access from within the class itself should be encapsulated with getters/setters. So it's really no use to insert them from the first. Perhaps you can convince your colleagues if you show them how easy it is with modern tools to add accessors when they're really needed!

Marco
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Write a getter and setter to each private field