• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Class get and set explanation

 
Beth Laguardia
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the error you are seeing? What do you expect the method to do?
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50278
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50278
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much.

I understand it now better than before.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50278
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic