-1

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.

  • 1
    Your class has no instance variables declared. If you don’t declare you can’t access. – Rana_S Sep 30 '22 at 00:59
  • 1
    Does this answer your question? [What is an instance variable in Java?](https://stackoverflow.com/questions/16686488/what-is-an-instance-variable-in-java) – Tim Moore Sep 30 '22 at 00:59
  • Yes, you have to declare variables *outside* of a method for them to be class or instance variables. You probably need to move `int[] frequency` out of the ctor as well. – markspace Sep 30 '22 at 01:18

1 Answers1

2

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
    }
...
Andrew S
  • 2,509
  • 1
  • 12
  • 14