Results 1 to 9 of 9
  1. #1
    jnjh is offline Member
    Join Date
    Mar 2011
    Posts
    12
    Rep Power
    0

    Default Confused about getters and setters

    Greetings,

    I am new to java and i am so confused about the getters and setters. plz tel what is the advantage of using getters and setters in code.

    Thanks

  2. #2
    Join Date
    Mar 2011
    Posts
    94
    Rep Power
    0

    Default

    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:

    Java Code:
    public class Person {
    
    	private int iAge;
    ...
    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;
    
            public int getAge() { return iAge; }
            public void setAge(int i) { iAge = i; }
    ...
    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.

    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.

  3. #3
    jnjh is offline Member
    Join Date
    Mar 2011
    Posts
    12
    Rep Power
    0

    Default

    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

  4. #4
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    618
    Rep Power
    4

    Default

    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.
    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;
    }
    I advice you to read a book it name is Head First Java. There are the more detail explain this material.
    Skype: petrarsentev
    http://TrackStudio.com

  5. #5
    MatthewAB is offline Member
    Join Date
    Mar 2011
    Posts
    5
    Rep Power
    0

    Default

    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.

  6. #6
    Join Date
    Mar 2011
    Posts
    94
    Rep Power
    0

    Default

    Quote Originally Posted by jnjh View Post
    Map map = new HashMap();
    Person.setAge(22);
    map.put(Person.getAge(), 1);

    can we do like this???
    Thanks in advance
    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:

    Java Code:
    Person p = new Person();
    p.setAge(22);
    map.put(p.getAge(), 1);
    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:
    int iTotalPeople = Person.getTotalPeople();
    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.

  7. #7
    jnjh is offline Member
    Join Date
    Mar 2011
    Posts
    12
    Rep Power
    0

    Default

    Thank u very much petr,MatthewAB, and FlipPoker...

  8. #8
    Skiller is offline Member
    Join Date
    Jan 2011
    Posts
    67
    Rep Power
    0

    Default

    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.

  9. #9
    vivek6569 is offline Member
    Join Date
    May 2011
    Location
    bangalore
    Posts
    4
    Rep Power
    0

    Default 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

  1. Setters and Getters and the counter exercise
    By gnng in forum New To Java
    Replies: 8
    Last Post: 03-27-2011, 08:48 PM
  2. Do getters & setters effect the performance?
    By malaguena in forum New To Java
    Replies: 6
    Last Post: 03-12-2011, 07:46 PM
  3. Positions and values, getters, setters
    By Malus in forum New To Java
    Replies: 10
    Last Post: 01-23-2010, 05:55 PM
  4. Getters and Setters
    By lheviathan in forum New To Java
    Replies: 4
    Last Post: 11-02-2009, 01:47 AM
  5. Getters and Setters
    By Charliestons in forum New To Java
    Replies: 10
    Last Post: 09-12-2008, 10:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •