wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Using get/set vs. direct access to private variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Using get/set vs. direct access to private variables" Watch "Using get/set vs. direct access to private variables" New topic
Author

Using get/set vs. direct access to private variables

Michael Bruesch
Ranch Hand

Joined: Sep 23, 2001
Posts: 158
When you are accessing a private instance variable from within the same class, is it "more correct" to access the variable directly like

or to access it through the get/set methods like

It would seem to me that direct access would save a few milliseconds.

Thanks!!


Michael J Bruesch<br /><i>I code, therefore I am.</i>
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

Generally, if you are within the same class, the convention is to access the instance variable directly. An exception to this might be if the getter did some sort of error checking, and you wanted that error checking to occur each time you use the instance variable; then that work could be centralized in the getter, and the getter would be used. (And you'd put very descriptive JavaDoc comments on the actual instance variable so the next person that comes along knows to use the getter )
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

Although if you use access methods you can allow subclasses to override the behaviour.

I read a post in the Swing forum about someone who actually needed the base class to call methods, instead of using private variables. He ended up copy-pasting over 90% of the class he actually wanted to subclass.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Michael Bruesch
Ranch Hand

Joined: Sep 23, 2001
Posts: 158
Have any time studies been performed to analyze the difference? I've done limited searching online on this topic but can't find anything conclusive.
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

Originally posted by Michael Bruesch:
Have any time studies been performed to analyze the difference? I've done limited searching online on this topic but can't find anything conclusive.


I assume you means in terms of the time difference? I'm not sure if any such studies have been done. But, given todays processors, you are talking nanoseconds difference; likely just a couple of nanoseconds. And clear, understandable, and easy to maintain code should be a priority over extreme optimization. In the end, clear, understandable, and easy to maintain code will save hours (if not days or weeks) when changes or maintenance needs to be done. And that's optimization you can use.

There's a famous quote by Donald Knuth, who is a computer science guru, that states "We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil." (Knuth, Donald. Structured Programming with go to Statements, ACM Journal Computing Surveys, Vol 6, No. 4, Dec. 1974. p.268.) You can read more about optimization in this wikipedia article.
Michael Bruesch
Ranch Hand

Joined: Sep 23, 2001
Posts: 158
Oh yes, I will almost always choose readability over saving a few nano or milliseconds, but since they both seem equally readable to me I figured I'd see which one performed better.

Thanks for all the replies!
arun nohwar
Greenhorn

Joined: Aug 05, 2008
Posts: 16
If the class may need to be promoted to a bean in near future with some functionality added in set(), then to avoid future editing, we can go for using get/set.
John M Morrison
Greenhorn

Joined: Jul 25, 2005
Posts: 21
State variables of classes should always be declared private, unless they are constants.

This is the way of saying "NO USER-SERVICEABLE PARTS INSIDE"

You can protect your state variables from such things as illegal initializations. Imagine this class

class Date
{
public int month;
public int day;
public int year;
.
.
.
(other entrails)
}

class Foo
{
d = new Date();
d.month = -5;
d.day = 433343;
d.year = 1900;
.
.
.
//more inept client code.
}

Can you imagine anything uglier?


By allowing access to state variables, you break encapsulation and you destroy the integrity ofyour class. You also foreclose the possibility of EVER changing the implementation of the class.

Keep your state variables PRIVATE!
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4168
    
  21

Originally posted by John M Morrison:
Keep your state variables PRIVATE!


Great advice, but the original question was about referencing a variable in the same class that it is declared in.

I am gonna throw my 2 cents in with using the get/set method calls rather than direct access. If those getters and setters can be overriden by child classes, then I would expect any behavior dependent on the properties those accessors represent to reflect the new behavior the child wants.


Steve
 
Consider Paul's rocket mass heater.
 
subject: Using get/set vs. direct access to private variables
 
Similar Threads
Calling variable directly vs getter method
what are setters and getters for
Instance variable of type some class in another class
Using Objects and variables from a different Class
masterexam