• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

getter method and setter method

 
Sivakumar Janardhanan
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I want to know briefly about setter method and getter method.

Actually where do we use these method?
And how is integrated with some other class?


Somebody explain about that things


thanks
sivakumar
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15369
40
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This question is not really about the SCJP exam, it's more a Java beginners question. I will move this to a more appropriate forum for you.
 
Albareto McKenzie
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They are used for getting and setting attributes values. Imagine this class


And then you can have another class that prints the color and the model:



Try out this and see what happends.

(I hope not typos inside, I have coded it directly here)
 
santhosh.R gowda
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to know briefly about setter method and getter method.

Actually where do we use these method?
And how is integrated with some other class

hi Janardhan,
As we know in class some instance variables (Object varaibles)are declared. and when you expose your class means if some body uses your code in their program like import statement . so they can access your instance variable or cahange the magnitude(value) of the varaible that you dont like means that varaible will cause the error in other methods of your program for example you have declared class by name cat and you had declared int sieze which is public so some body uses your code in their program can make size = negative value as you know cat size value can never be negetive (States must be legal) so the one solution is by making the instance varaible private .by doing so you are breaking OO concepts the value never get inherited to your class so we are using public setter methods to assign the perticular value to instance variable and gettermethods to get the values so by making methods public we can set and get the instance variables.this is what we called Encapsulation one of the feature of OO concepts where we binding data(instance variable) and functions(methods) into a single unit.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49466
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Albareto McKenzie wrote:They are used for getting and setting attributes values. Imagine this class
I don't know whether any of my enhancements to your code would run or compile; try them and see. You will have to create a YuckyColourException class, maybe extending IllegalArgumentException. But the code now demonstrates that "set" methods can test their input to validate it before accepting it. And similarly for "get" methods.
 
Sivakumar Janardhanan
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your Reply
 
Albareto McKenzie
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I don't know whether any of my enhancements to your code would run or compile; try them and see. You will have to create a YuckyColourException class, maybe extending IllegalArgumentException. But the code now demonstrates that "set" methods can test their input to validate it before accepting it. And similarly for "get" methods.


Yes, you can do validations in the getters and setters, but that was a really simple example of how this kind of methods work

Offtopic: Is skybluepink the new color for Alfa Romeo's cars?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49466
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At risk of having to move this thread to "Meaningless Drivel" . . .

No, the new colour is beigechartreusegrey
 
Campbell Ritchie
Sheriff
Pie
Posts: 49466
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sky blue pink is a non-existent colour often used by British school children; I knew about it when I was at school, and so did my daughters. It is used mostly for joking about . . .

"What colour should it be then, sky blue pink?"
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic