• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Field Access VS getter/setter-Access

 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Vijitha Kumara
Bartender
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Campbell Ritchie
Sheriff
Pie
Posts: 49472
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49472
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, now I am convinced preferring the getter/setter.

thanks!
 
Campbell Ritchie
Sheriff
Pie
Posts: 49472
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by nimo frey:
Okay, now I am convinced preferring the getter/setter.

thanks!


 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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"...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic