Results 1 to 9 of 9
- 03-21-2011, 12:30 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 12
- Rep Power
- 0
- 03-21-2011, 12:52 PM #2
Member
- Join Date
- Mar 2011
- Posts
- 94
- Rep Power
- 0
Getters and setters hide the implementation details of your class. They provide an API for users to modify your class data. For example, look at this class:
We have declared a private attribute to hold a person's age. Since it is private, users of our class cannot modify it. In fact, they can't even access it (yet). So now we add a getter and setter:Java Code:public class Person { private int iAge; ...
The two methods above are public methods, so users of our class can call them. Users now have a way to get the value of age and set it. By providing these two methods, we give an API to users who want to use this class. We also control this access. For example, if we want to count the number of times the age has been set, we can put a static counter in the setAge() method. Plus, if we no longer want users to access the age attribute, we can simply remove the getAge() and setAge() methods.Java Code:public class Person { private int iAge; public int getAge() { return iAge; } public void setAge(int i) { iAge = i; } ...
Without the getter and setter methods above, we'd have to make the iAge attribute public. Then users could freely access the value. But that's just not good practice because you have no control over that access.
Hope this helps.
- 03-21-2011, 12:59 PM #3
Member
- Join Date
- Mar 2011
- Posts
- 12
- Rep Power
- 0
Thank a lot FlipPoker.... now i understand the concept....
lets assume that we have a HashMap
Map map = new HashMap();
Person.setAge(22);
map.put(Person.getAge(), 1);
can we do like this???
Thanks in advance
- 03-21-2011, 01:01 PM #4
It is offered the more flexible to rule data in your objects. for example. you want that some user sets only not null value in you object.
I advice you to read a book it name is Head First Java. There are the more detail explain this material.Java Code:private String id; public void setId(String id) { if (id == null) { throw new IllegalArgumentException("Param ID is not be null"); } this.id = id; }Skype: petrarsentev
http://TrackStudio.com
- 03-21-2011, 01:02 PM #5
Member
- Join Date
- Mar 2011
- Posts
- 5
- Rep Power
- 0
While doing a little research before answering you I found multiple articles that debated whether setters/getters were "evil/necessary" as far as a design standpoint. I realize your question doesn't go that in depth so I continued searching. I have been programming for less than 6 months so I didn't just want to run away with this one. On yahoo forums I found a description which describes the type of methods very well.
"Getters and setters are fuctions used in object oriented programming (OOP) to get and set object property values. Since you do not want to allow direct access to data fields of an object, you use functions to access the fields. For instance, if you have an object called CRect, you might have fields called ht and width which are private to the object. You then have your getters to retrieve the values, GetHt and GetWidth. Likewise with setters to change the values. One of the advantantages to this system is that you can make a property "ReadOnly"by not implementing a setter for that property. "
What are getters and setters in programming ,why they are used ? - Yahoo! Answers
My understanding coincides with the poster, setters and getters are mainly used for control of access to the data.
- 03-21-2011, 01:18 PM #6
Member
- Join Date
- Mar 2011
- Posts
- 94
- Rep Power
- 0
Since the setAge() and getAge() methods are not static, you need to instantiate a Person object before you can call those methods. Then you invoke those methods on that object:
In your original code, you are invoking the methods as if they were static. Static members and data belong to the class, not any particular instance. For example, you could define a static member called getTotalPeople() that returned the number of Person objects created. You would invoke it like this:Java Code:Person p = new Person(); p.setAge(22); map.put(p.getAge(), 1);
For static members and data, you use the Class name (i.e. Person) to invoke them) since the member doesn't apply to a particular object. It applies to the entire class.Java Code:int iTotalPeople = Person.getTotalPeople();
- 03-21-2011, 01:24 PM #7
Member
- Join Date
- Mar 2011
- Posts
- 12
- Rep Power
- 0
Thank u very much petr,MatthewAB, and FlipPoker...
- 03-21-2011, 02:53 PM #8
Member
- Join Date
- Jan 2011
- Posts
- 67
- Rep Power
- 0
There's another good reason to use getters and setters that I'm surprised no one else mentioned: maintainability. If you ever work on a large project with lots of people, using getters and setters can be a massive time saver if you ever need to change the underlying data because you can change the data without affecting other peoples code.
For instance, with FlipPoker's example a Person has an Age value, if you decide that you need to change that to a birth date so the Age is correct in a years time then you only need to change the getAge and setAge functions when you change iAge from an int to a date. If iAge was just a public member then you'd need to change any code in the project that used the iAge variable.
- 05-03-2011, 07:12 AM #9
Member
- Join Date
- May 2011
- Location
- bangalore
- Posts
- 4
- Rep Power
- 0
getter and setter
getter and setter method are used to retrieve and manipulate private variable in different class. a "getter" method does as it name suggested , retrieves a the attribute of the same name. a setter method allows you to set the value of the attribute.
Last edited by vivek6569; 05-03-2011 at 07:16 AM.
Similar Threads
-
Setters and Getters and the counter exercise
By gnng in forum New To JavaReplies: 8Last Post: 03-27-2011, 08:48 PM -
Do getters & setters effect the performance?
By malaguena in forum New To JavaReplies: 6Last Post: 03-12-2011, 07:46 PM -
Positions and values, getters, setters
By Malus in forum New To JavaReplies: 10Last Post: 01-23-2010, 05:55 PM -
Getters and Setters
By lheviathan in forum New To JavaReplies: 4Last Post: 11-02-2009, 01:47 AM -
Getters and Setters
By Charliestons in forum New To JavaReplies: 10Last Post: 09-12-2008, 10:57 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks