简体   繁体   中英

The local variable real may not have been initialized

I have written the complex.java code for complex numbers. But it gives the error "The local variable real may not have been initialized" on eclipse. Can't figure out what's wrong. The code is as follows. Any help would be appreciated.

import java.lang.Math; 

public class Complex { 
    public double real; 
    public double comp; 

    Complex(double real, double comp) {  
    this.real = real; 
    this.comp = comp; 
    }

    Complex() { 
    this.real = 0; 
    this.comp = 0;
    }


    public double getReal() {
        return real;
    }

    public double getImg() {
        return comp;
    }


    public Complex add(Complex a) { 
        return new Complex(a.getReal() + this.real, a.getImg() + this.comp);
    }


    public static Complex add(Complex a, Complex b) { 
        double real = a.real + b.real;
        double comp = a.comp + b.comp;
        Complex sum = new Complex(real, comp);
        return sum;
    }

    public double getABS() { 
        return Math.sqrt(real*real + comp*comp);
    }

    public double getABSSqr() { /* get the squre of absolute */
        return (real*real + comp*comp);
    }


    public Complex mul(Complex a) { 
        return new Complex(a.getReal()*this.real-a.getImg()*this.comp, a.getReal()*this.comp+this.real*a.getImg());
    }

    public static Complex mul(Complex a, Complex b) { 
        double real = a.real*b.real-a.comp*b.comp;
        double comp = a.real*b.comp+b.real*a.comp;
        Complex mul = new Complex(real, comp);
        return mul;
    }

    public Complex squre() { 
        double real = real*real-comp*comp; //THIS IS WHERE ERROR APPEARS FOR real*real
        double comp = 2*real*comp;  //THIS IS WHERE ERROR APPEARS FOR comp                  
        Complex squre = new Complex(real, comp);
        return squre;
    }   

    public void display() { 
    System.out.println(this.real + " + " + this.comp + "j");
    }
}

You need to use this.real and this.comp on the RHS of that statement. That's because you've local variables with the same name in that scope. To differentiate those from the instance variables, you need to use this .

double real = this.real*this.real-this.comp*this.comp;
double comp = 2*real*this.comp; // this.comp refers the instance variable comp and real refers the real declared in the previous line

Therefore, if you just give real , it'll try to use the real on the left hand side itself, which is uninitialized yet and thus the error.

    double real = real*real-comp*comp; //THIS IS WHERE ERROR APPEARS FOR real*real
    double comp = 2*real*comp;  //THIS IS WHERE ERROR APPEARS FOR comp  

This refers to the same declared variable on the left hand side of the equation. You need to make it either use a different name for the local variable, or use this.real*this.real on the right hand side.

Great question! In the line:

double real = real*real-comp*comp;

The real variable that is referenced in the real*real expression is actually the local real variable that you just declared, rather than the real field of the object, as you might think. The local variable takes precedence in terms of scope. If you want to reference the field, you will need to use: this.real to be explicit about which scope you are talking about.

For example:

double real = this.real * this.real - this.comp * this.comp;
double comp = 2 * this.real * this.comp;

Alternatively, you can avoid the problem by using different names for the local variables, so that the compiler cannot be confused:

double r = real * real - comp * comp;
double c = 2 * real * comp;
return new Complex(r, c);

Or you can remove the temporary variables and put the calculation on one line:

public Complex square() { 
    return new Complex(real * real - comp * comp, 2 * real * comp);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM