Hi
Here is a piece of code from an example in RHE
public class complex
{
private double real,imaginary;
public complex(double r,double i)
{
real = r; imaginary=i;
}
public complex add(complex c)
{
return (new complex(real+c.real,imaginary+c.imaginary));
}
}
class client
{
public static void main(
String args[])
{
complex c1 = new complex(1,2);
complex c2 = new complex(3,4);
System.out.println("before c3");
complex c3 = c1.add(c2);
System.out.println("after c3");
//double d = c3.real;
}
}
I have two questions regarding this
a) It compiles but gives a runtime error of java.lang.NoSuchMethodError in
thread main. Can anyone explain why?
b)RHE mentions that "any instance of complex can access the private variable real". However if the last line
double d = c3.real
is uncommented the code does not compile. c3 is an instance of complex so I thought it should be able to access its real variable using c3.real
Thanks in advance for your help
------------------
Bos Indicus