Hello guys, happy new year.
Well, a very basic question. I make a class objekt that lookes
something like this :
public class circle {
public static final PI = 3.14159;
public double r;
public double area(double r) {
return r*r*PI;
}
}
and then from somewhere else I do :
circle.r = 5;
double area = circle.area(); <--- this gives me a compilation error,
it wants me to specify circle.area(5). I thought
that it was legitamet code to leave it at (). Is'nt it what object
oriented code was all about, i have already defined the objects
variable r to bee 5, shouldent need to further specify
circle.area(5).
Thank you for any response in advance.
On Dec 31, 2:55 pm, mohed.hai...@gmail.com wrote:
> Hello guys, happy new year.
> Well, a very basic question. I make a class objekt that lookes
[quoted text clipped - 4 lines]
> public static final PI = 3.14159;
> public double r;
This defines a member variable named 'r'.
> public double area(double r) {
This defines a method taking a parameter named 'r', unrelated to the
member variable. You probably meant
public double area () {
or more conventionally
public double getArea () {
> return r*r*PI;
> }
[quoted text clipped - 11 lines]
> variable r to bee 5, shouldent need to further specify
> circle.area(5).
In Java, local variable names and parameter names hide member
variables with the same name. The following program is exactly
equivalent to your original program, but illustrates why you get an
error:
public class circle {
public static final PI = 3.14159;
public double r;
public double area(double r2) {
return r2*r2*PI;
}
}
-o
Patricia Shanahan - 01 Jan 2008 00:12 GMT
> On Dec 31, 2:55 pm, mohed.hai...@gmail.com wrote:
>> Hello guys, happy new year.
[quoted text clipped - 4 lines]
>>
>> public static final PI = 3.14159;
In addition to Owen's comments, PI needs a type. On the other hand, you
should probably get rid of it. Math.PI is a far closer approximation to pi.
Patricia