简体   繁体   中英

How do I properly reference the variable from another method in java?

I don't know how I'm supposed to reference the variables in the .toString() method. I've been working on ways to do it. It's public so I don't see why it's not being shared. import java.util.Scanner;

class Donation {

public static void main(String[] args) {

    Donation uw1 = new Donation("University of Washington", 600.75, true);

    Donation uw2 = new Donation("University of Washington", 40, true);

    Donation sj = new Donation("Snap Judgment", 30, false);

    Donation tal = new Donation("This American Life", 40, true);

    Donation mc = new Donation("Microphone Check", 99.99, false);

    System.out.println(uw1.toString());
    System.out.println(uw2.toString());
    System.out.println(sj.toString());
    System.out.println(tal.toString());
    System.out.println(mc.toString());
}

public Donation(String organization, double amount, boolean tax) {
    double v= amount;
    String org = organization;
    boolean b= tax;

}

public String toString(){
    if (!v){
        return "$"+v+": "+org;
    }
    else if (b) {
        return "* $" + v + ": " + org;
    }
    else{
        return "error";
    }
}

You set local variables in your constructor:

public Donation(String organization, double amount, boolean tax) {
    double v= amount;
    String org = organization;
    boolean b= tax;
}

and this does you know good since these variables exist only within the constructor and once the constructor ends, the variables disappear with it, and this is the crux of your problem. Instead, declare those variables outside of the constructor and in the class.

public class Donation {

    private double v;
    private String org;
    private boolean b;

Then yes, set the fields within the constructor but don't re-declare them.

public Donation(String organization, double amount, boolean tax) {
    v= amount;
    org = organization;
    b= tax;
}

Then you can use the values held by these fields within your toString method.

You cannot share variables across methods, you can however use fields to do this. You can assign these fields in the constructor, replacing the variables you've defined there now, and then reference these fields in the toString() method.

Variables declared within functions are 'local variables'. Their lifetime is the execution of the method.

So v , org and b only exist in your Donation constructor. By the time you call toString they no longer exist

To have variables whose lifetime is the same as the lifetime of your object you need to create a field on your object.

class Donation {
  private final double v;
  
  public Donation(double value) {
     v = value;
  }

  public toString() {
     return "$" + v;
  }
}

v is private and final. That means that it can't be changed after the object is created, which makes it easier to think about how your object behaves, and can't be seen outside your class, which means that you can change your class without affecting the other classes which use it.

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