aspose file tools*
The moose likes Java in General and the fly likes Field Access VS getter/setter-Access Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Field Access VS getter/setter-Access" Watch "Field Access VS getter/setter-Access" New topic
Author

Field Access VS getter/setter-Access

nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
What is better?

To have this:



and set/get the value a via the methods, such as:



Or to have only this:



and get/set the value only via the value itself, such as



At the moment, I prefer the value-Type becaue I have not much code in my source (no getter/setters). Are there any (dis)advantages (performance, file size,..)? What should I prefer?
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Originally posted by nimo frey:
What is better?

To have this:



and set/get the value a via the methods, such as:



This methods helps to set and get the instance variable 'a' of class A, this method can be invoked by user class (The class which uses A`s object)

Originally posted by nimo frey:

Or to have only this:



and get/set the value only via the value itself, such as



At the moment, I prefer the value-Type because I have not much code in my source (no getter/setters). Are there any (dis)advantages (performance, file size,..)? What should I prefer?


This code , inside a class, initializes an instance variable with some constant string .

So this are not relevant to each other , or I`m not getting it right then , pl correct me ?
[ August 29, 2008: Message edited by: Sagar Rohankar ]

[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3860

If you have outside sources using your class you have to have the instance variables private and provide the public getters/setters with validations(if necessary).


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
As Vijatha Kumara says, you can include validation in a "set" method. What you are doing is conforming to the object-oriented practice of "information hiding." You are keeping the value of your fields hidden, only allowing a value to be see via a method in the class' public interface.
You can also use copying in a "get" method; returning a copy of the field, if it is a mutable reference type.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
If you are using such methods inside the same class, I am not sure that it matters whether you call a set method or use =. If you are in a subclass, you would have to use get and set methods because the private fields are not directly available.
nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
Okay, now I am convinced preferring the getter/setter.

thanks!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Originally posted by nimo frey:
Okay, now I am convinced preferring the getter/setter.

thanks!


nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
Look at the article standing for avoiding getters/setters:

http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html?page=1
Christopher Young
Ranch Hand

Joined: Nov 02, 2007
Posts: 63
I like using getter/setter methods even in the own class because to me, it's just a lot more readable to see

int a = getFoo();

than say

int a = foo;

In the latter, you have to remember the names of all the fields in the class, and it can be a little unclear on what foo is, etc

That article does make sense however.


Technology can never substitute for knowledge.
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1836
    
    7

Originally posted by nimo frey:
Look at the article standing for avoiding getters/setters:

http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html?page=1


Ah yes, another excellent article by Allen Holub.
Be sure not to miss out on other gems like "Why extends is evil" and "Just say no to XML"...


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Field Access VS getter/setter-Access