This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Methods to access objects? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Methods to access objects?" Watch "Methods to access objects?" New topic
Author

Methods to access objects?

Derek White
Greenhorn

Joined: Jul 24, 2008
Posts: 14
I have noticed a fair amount of classes use such code:



Is this preferred over making "str" publicly accessible?
If so, please explain why..
ahalya priya
Greenhorn

Joined: Jan 21, 2009
Posts: 10
Hi
In java to implement encapulations, all variables are declared private and methods those set and get value of particular variables are declared public. So that all other are not able to access that variables directly.

I hope you understand....

Derek White
Greenhorn

Joined: Jul 24, 2008
Posts: 14
I don't understand why it is unsafe for the member to be accessed directly..

Is there some documentation for this?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Nutshell version: encapsulation.

Getters and setters allow additional functionality to be included--as a trivial example think of lazy instantiation of expensive properties. By encapsulating property access behind methods no code needs to change if, say, a publicly-accessible property now needs additional functionality provided by a getter/setter method.

They also allow trivial discovery by tools of properties *meant* to be accessible (by looking for methods prefixed with get/set).
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15292
    
    6

Derek White wrote:I don't understand why it is unsafe for the member to be accessed directly..

Is there some documentation for this?


It's not always unsafe and will depend on your implementation. There's the camp that believes that business logic belongs within the JavaBean. If that is the case, often times when setting or getting a property, business rules must be applied to them first. So there is additional logic in these mutator methods to achieve this. The other reason getters/setters are fairly common is because of libraries that work off JavaBeans and reflection like Hibernate, for example. Hibernate requires getters and setters for every persistent property of the object to work. This is also true many other libraries out in the java world.

There is actually quite a bit of debate regarding private/public variables and mutator methods. Googling and searching this site will provide more information. Unfortunately there isn't really a best practice. More often than not you are going to find yourself requiring getters/setters for various reasons.


GenRocket - Experts at Building Test Data
Derek White
Greenhorn

Joined: Jul 24, 2008
Posts: 14
Additional functionality? As in restrictions and such?



Or maybe to fire an event when the String in question is changed?
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15292
    
    6

Derek White wrote:Additional functionality? As in restrictions and such?



Or maybe to fire an event when the String in question is changed?


Yep. You got it. Also note that is can also depend on what you are developing for. When designing a system where a lot of people will be using it and programming against your API it is really important to restrict access and control access to important data. Whether that restriction rests solely on the objects themselves or are proxied through other objects is up to you. But if you need to be concerned about how a property is set (and at times retrieved) from an object, you need to control that via methods, and not expose the variables directly.

It might also be helpful to read the JavaBean specification.
Derek White
Greenhorn

Joined: Jul 24, 2008
Posts: 14
Thank you very much!

It's something I've always wondered about and now I finally understand .
arulk pillai
Author
Ranch Hand

Joined: May 31, 2007
Posts: 3216
This will also prevent a varible getting changed with an invalid value. For example, you can check for null or zero, etc prior to assigning if you use encapsulation.


Java Interview Questions and Answers Blog | Amazon.com profile | Java Interview Books
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15292
    
    6

arulk pillai wrote:This will also prevent a varible getting changed with an invalid value. For example, you can check for null or zero, etc prior to assigning if you use encapsulation.


Yea, I think we covered that, but thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Methods to access objects?
 
Similar Threads
references and method parameters
How to pass value from one file to anaother
var access
shadowing variables
fast deep copy