简体   繁体   中英

In Java, how do I access class variables from a method within that class?

This feels like a very basic question and I'm not sure how to phrase it better, otherwise I would've never made this post. I've been trying to google this for close to an hour now. Here's the relevant code.

public class Histogram {
public Histogram(int lowerbound, int upperbound) {
    int frequency[] = new int[10];
}

public static void main(String args[]) {
    Histogram histo = new Histogram(0, 5);
    //this section adds a bunch of elements to the histogram, commented out for readability
    System.out.println(histo);
    }

public boolean add(int i) {
    if (i > lowerbound && i < upperbound) {
        ...
    }
}

I cannot figure out how to access the lowerbound and upperbound passed into the Histogram object. I'm trying to use the "add" method to add to each tier of the histogram. It's in the last block, "if (i > lowerbound && i < upperbound)" where I'm getting the error.

The constructor

public class Histogram {
    public Histogram(int lowerbound, int upperbound) {
        int frequency[] = new int[10];
    }
...

has parameters lowerbound and upperbound , but those parameters are scoped to the constructor, so once the constructor exits those parameters are out of scope. (Same for frequency defined within the constructor.)

Instead add fields to the class, and initialize those fields as part of the custructor:

public class Histogram {
    private int lowerbound;  // define the class field
    private int upperbound;  // define the class field
    public Histogram(int lowerbound, int upperbound) {
        this.lowerbound = lowerbound; // initialize this instance's field value from the parameter that happens to also be named `lowerbound`
        this.upperbound = upperbound; // initialize this instance's field value from the parameter that happens to also be named `upperbound`
        int[] frequency = new int[10]; // when the constructor exits `frequency` goes out of scope and no longer accessible
    }
...

Using the same parameter name as the class field name requires the use of this to distinguish between the constructor parameter and the class field. This alternative might help to clarify:

public class Histogram {
    private int lowerbound;  // define the class field
    private int upperbound;  // define the class field
    public Histogram(int lower, int upper) {
        lowerbound = lower; // initialize this instance's field value
        // this.lowerbound = lower;  Could still use `this`, but it's not needed since the constructor parameter and the class field are different names
        upperbound = upper; // initialize this instance's field value
        // this.upperbound = lower;  Could still use `this`, but it's not needed since the constructor parameter and the class field are different names
        int[] frequency = new int[10]; // when the constructor exits `frequency` goes out of scope and no longer accessible
    }
...

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