• Post Reply Bookmark Topic Watch Topic
  • New Topic

getter and setter methods.  RSS feed

 
sachin yadav
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,

i am very confused on the getter and setter methods. Can somebody please explain me why we use getter and setter methods and how they are helpfull to us.

Regards,
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
 
Srinivas Kalvala
Ranch Hand
Posts: 257
Firefox Browser Hibernate Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Encapsulation is one of the OO feature, which will hide the data from external world. One can't access the data directly.

getter and setter method not only provides one way to hide the data, but also will give control to you to perform the validations.

For example, in business and real time applications, there are certain validations, like age must be greater than 3 years. If you have getter and setter methods, you can check before assignment or null check.

Explore more ...
 
sachin yadav
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for your time, but can you explain a bit more on that.
 
Tim LeMaster
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Compare these two classes - in the first example if you wanted to disallow certain values for age - you are going to have to refactor the Person class to put a setter in front of it. Where as in the second class all you have to do is add checkes before the "this.age = age" call - of course this method doesn't throw any exceptions so you might have to make more changes than that.

Encapsulation lets you hide the internal struture from the outside world and force them to come from through an API. Then if you can keep the API intact you can make changes internally without anyone else noticing. Its hard to do with something as simple as an Age - but what if you wanted to change how it was stored internally - with your public int age; you can't remove this or rename this variable safely - anyone could be using it. with get/setAge as long as you can still take and return an int - the user doesn't care what you do internally.
 
Srinivas Kalvala
Ranch Hand
Posts: 257
Firefox Browser Hibernate Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Sachin, Please see, HOW TO POST GOOD QUESTIONS.

Please try yourself to explore ...

Here we are to learn and explore ...
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well, i dont mind helping you, not like im giving you an answer to a test..

ok,



i hope this helped some...

Justin

[ September 19, 2006: Message edited by: Justin Fox ]
[ September 19, 2006: Message edited by: Justin Fox ]
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The java classes with getter and setters are very much useful for transfering date or to represent a set of data.

Suppose you want to exactly replicate a addressbook in code.You can have a arraylist as the addressbook and can have a class Address with name and phone as private variables and you can have getter and setters for name and phone.use setters to popolate the address object and put in the addressbook (that is the arraylist) and continue with the approach to create address object for each address card , populate the object using the setters and set in the arryalist.Now you have a full address book and you can transfer this to any other interface which knows how to deal with the addressbook.
 
Chetan Parekh
Ranch Hand
Posts: 3640
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by sachin yadav:
hi all,

i am very confused on the getter and setter methods. Can somebody please explain me why we use getter and setter methods and how they are helpfull to us.

Regards,


Sachin, I suggest you that download Thinking In Java 2nd Ed from here and read it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!