aspose file tools*
The moose likes Beginning Java and the fly likes getter method and setter method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "getter method and setter method" Watch "getter method and setter method" New topic
Author

getter method and setter method

Sivakumar Janardhanan
Ranch Hand

Joined: Dec 11, 2008
Posts: 30
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

Joined: Aug 16, 2005
Posts: 14269
    
  21

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Albareto McKenzie
Ranch Hand

Joined: Apr 08, 2009
Posts: 298
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

Joined: Apr 06, 2009
Posts: 296
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.


Creativity is nothing but Breaking Rules
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39415
    
  28
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

Joined: Dec 11, 2008
Posts: 30
Thanks for your Reply
Albareto McKenzie
Ranch Hand

Joined: Apr 08, 2009
Posts: 298
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

Joined: Oct 13, 2005
Posts: 39415
    
  28
At risk of having to move this thread to "Meaningless Drivel" . . .

No, the new colour is beigechartreusegrey
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39415
    
  28
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?"
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: getter method and setter method