File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Class get and set explanation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Class get and set explanation" Watch "Class get and set explanation" New topic
Author

Class get and set explanation

Beth Laguardia
Ranch Hand

Joined: Jun 01, 2008
Posts: 34
Hello everyone,

I would like to understand how set and get works in the following program.

I do not understand why deleting the getRadius method creates an error. If i looked at it it seems that getRadius method does nothing but I know it does something that i cannot understand.

Can somebody explain to me how the following program works please?

Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

What is the error you are seeing? What do you expect the method to do?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
You dont have a Constructor for your class. Unless you make your methods static, or create a constructor for your class, you cant call methods from a class object that doesn't exist.

if you just make your methods all static, you can call the methods kind of like you are, but instead like so:



But if you want to make multiple circle objects, then you need to create a constructor(even if it is an empty one)

like so:



Justin Fox
[ June 27, 2008: Message edited by: Justin Fox ]

You down with OOP? Yeah you know me!
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

DO NOT MAKE ANY METHODS STATIC!

DO NOT WORRY ABOUT THE CONSTRUCTOR RIGHT NOW!

Justin, nothing you said seems relevant to Beth's problem to me; the program should work fine as-is, with no constructor. Let's just worry about one thing at a time.

Now, as to your question: you should be able to remove the getRadius() method entirely without having any effect on this program, since no code here calls that method. It doesn't matter what a method does if it never gets called!

When you say "new Circle()" you create a new Circle object. Imagine that an object is like a kitchen drawer with dividers in it. In one section, you put forks, in another, spoons, etc. For a Circle object, there's a divider where you can put a value called "radius". That divider corresponds to the "radius" instance variable.

The setRadius(x) method says "here's a value 'x', put it in the divider for storing the radius, please."

The getRadius() method asks for the value stored in the radius divider. It could be used like

double radius = aCircle.getRadius();

Is this helping at all?


[Jess in Action][AskingGoodQuestions]
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
Dang, I thought you had to have a constructor to be able to use the 'new' operator.

That's weird .

Sorry, I guess I didn't read his/her post well enough, I thought he/she was getting an error with the code as is.

Sorry for the bad info. too, my mistake,

Justin
Beth Laguardia
Ranch Hand

Joined: Jun 01, 2008
Posts: 34
Thank you for all response. I appreciate it greatly.

Originally posted by Ernest Friedman-Hill:


Now, as to your question: you should be able to remove the getRadius() method entirely without having any effect on this program, since no code here calls that method. It doesn't matter what a method does if it never gets called! Yeah, I was able to remove it now without a problem unlike yesterday. It's an error on my part. Sorry po.



The getRadius() method asks for the value stored in the radius divider. It could be used like

double radius = aCircle.getRadius();

Is this helping at all?


Yes sir, i am beginning to understand a bit. I read the following from an ebook which i do not undersand though.

[Accessor (or observer) methods read property (ie. field variable) values and are conventionally named getFoobar() or whatever the property is called.

Mutator (or transformer) methods set property values and are often named setFoobar() etc. Mutators can be used to ensure that the property's value is valid in both range and type.

from that ebook it asked me to create a program with setRadius, getRadius, and calcDiameter. Why do i need to use getRadius when In my program I can get the diameter without using the getRadius.

Or I should create a program with getRadius on it? But how? Can you please help me sir? What's the importance of it?
[ June 28, 2008: Message edited by: Beth Laguardia ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38481
    
  23
The importance of it is as an exercise so you can see how "get" and "set" methods work.

Change "double radius;" in the Circle class to "private double radius;"

Put this in your main method, after what you have at presentYou can see how the "get" methods tell you what the value of radius is; if you write what follows in the MakeCircle class anywhere it won't compile because you have got private access on "radius".But you can see how what I told you earlier will run nicely (as long as there aren't any spelling errors.

In a few weeks' time, when you have learned a bit more, you can work out how to put code in a "set" method which will prevent anybody making silly calls like this:
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38481
    
  23
Originally posted by Justin Fox:
Dang, I thought you had to have a constructor to be able to use the 'new' operator.
But you have got a constructor. Remember the compiler inserts a "default" constructor if you don't write your own.
Beth Laguardia
Ranch Hand

Joined: Jun 01, 2008
Posts: 34
Thank you very much.

I understand it now better than before.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38481
    
  23
Originally posted by Beth Laguardia:
Thank you very much.

I understand it now better than before.
Well done. We do try to help and, very occasionally, we succeed!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Class get and set explanation