I created a class Complex to do Complex operators. Inside the Complex class I made a method add which uses two Complex class parameters. But when I create two instances in main() and then try to add them.
public class ComplexNo {
public static void main(String[] args){
Complex c1=new Complex(1,1);
Complex c2=new Complex(2,3);
Complex cAdd=Complex.add(c1,c2);
}
}
class Complex{
float real; float imag;
Complex(float a, float b){
real = a;
imag = b;
}
Complex add(Complex a, Complex b){
return new Complex(a.real+b.real, a.imag+b.imag);
}
}
It shows the error "Non-static method 'add(Complex, Complex)' cannot be referenced from a static context". It is corrected by adding static keyword. How does it help?