• Post Reply Bookmark Topic Watch Topic
  • New Topic

confusion over Getters(accessors) and Setters(mutators).  RSS feed

 
akshay naidu
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i don't understand how Getters and Setter help you get and set things.

Head First Java pg.no. 79 says
A Getter's sole purpose in life is to send back,as a return value, the value of whatever it is that particular getter is supposed to be getting.
And Setter lives and breathes for the chance to take an argument value and use it to set the value of an instance variable.


help me understand the above lines with the help of codes below.



Thanks.
 
Dave Tolls
Ranch Foreman
Posts: 3056
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What don't you understand about that bit of code?

Take the getter first:


What about that is causing you problems?
 
Jude Niroshan
Ranch Hand
Posts: 132
5
Eclipse IDE Java Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi akshay,
I agree with Dave Tolls. This is very primilinary technique we use in our day-to-day developments. Basically we use them to keep our hundreds of data as inside a capsule. As they said, this is serialize nicely.
Please refer to http://stackoverflow.com/questions/1727603/places-where-javabeans-are-used more about getters & setters.





Hope this can be helpful.
Regards,
Jude
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jude Niroshan wrote:Please refer to http://stackoverflow.com/questions/1727603/places-where-javabeans-are-used more about getters & setters


Jude, please do not re-direct OP to look for an information in different source, as OP came to the Ranch to get some help.
Beside that, information found at your given link is not related with OP's topic, and it is very difficult to read.
 
Jude Niroshan
Ranch Hand
Posts: 132
5
Eclipse IDE Java Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jude, please do not re-direct OP to look for an information in different source,

Sorry bro....i didn't know something like that....
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jude Niroshan wrote:
Jude, please do not re-direct OP to look for an information in different source,

Sorry bro....i didn't know something like that....


Don't get me wrong, I just mean, that information provided over there, not necessarily correct, as people there shares their thoughts.

Would be much better, if you would provide something official, more trusted.

Some source like http://docs.oracle.com/javase/tutorial/java/javaOO/methods.html would be ideal, with some simple explanation what getter and setter methods means in general.
So OP could understand himself.
 
Tim Cooke
Marshal
Posts: 4041
239
Clojure IntelliJ IDE Java
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:Jude, please do not re-direct OP to look for an information in different source, as OP came to the Ranch to get some help.

There's nothing wrong with pointing folks to some other resource outside of the Ranch, we link to stackoverflow, and many other places, quite a lot if we think the resource is useful. Now, if you don't think the linked resource is useful, or even misleading, then it's ok to say so and suggest something else or provide clarification.

The Ranch is just a collection of guys and gals who love all things about programming and computers. We love it so much we feel compelled to talk about it with strangers on a daily basis, which is how this forum survives and thrives.
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good point, probably I wasn't clear enough.
I just found that information a bit too difficult for OP to read, as he couldn't distinct the certain information.

Sorry for being not clear enough.
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
akshay naidu wrote:


akshay, in simple language, getter method gets value for a caller of method
for example:


So, needValue calling a method getMeValue() to get the value for him. After the execution of method, needValue gets some return of type int (whole number).



By calling this method, you can update value of "brand" (or set a new value, it's might easier to understand for now). Within the brackets you have to specify that value (String within the double quotes, for numbers you don't need double quotes).

for example

so, after execution, brand gets value,


Try to ask, what any other doubts do you have about it.



 
akshay naidu
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello everyone.
i am new to java, in fact am new to programming,
trying to learn java from head first java.,so this basic stuff which is crystal clear to you is a lot confusing for me
 
akshay naidu
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what's the use of
in
and what value is getter going to get and how?
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
akshay naidu wrote:hello everyone.
i am new to java, in fact am new to programming,
trying to learn java from head first java.,so this basic stuff which is crystal clear to you is a lot confusing for me


That's why we are all here to help you.
Try to express yourself what kind of doubts do you have.

My suggestion would be, do not try to go through the book quickly, read chapters more than once.
Do not skip exercises, even if some of them looks obvious to you.

 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
akshay naidu wrote:what's the use of
in
and what value is getter going to get and how?


word "String" in this context means the type of the value this method returns.

it is an actual return statement, which means, the method returning a value to a caller.

remember, we called method to

So now, we can get this value, what was set up before.

 
akshay naidu
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so is returning a value to
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
akshay naidu wrote:so is returning a value to


No.

sets the value to a variable.

returns the value, which has been set up by setValue("value");
 
akshay naidu
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok,
let me try Encapsulating goodDog class program from the book.,i'll relate what you said to the program,may be that will give me a better understanding.
i'll post if i get any success in understanding it else will come up with a better question
thanks a lot for helping
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
akshay naidu wrote:ok,let me try Encapsulating goodDog class program from the book.,i'll relate what you said to the program,may be that will give me a better understanding.


In my opinion, HFJ it's a bit too difficult book to start with. It is a perfect book to extend your knowledge and move on.
I'm sure someone else has different opinions. But if you have a chance to get it, I'd recommend you go first through "JAVA for everyone: Late Objects" by Cay S. Horstmann.

But if you think it's not a case, then try to implement that class from HFJ and see how you find it.




 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!