-1

i have 2 classes:

public class A {

    public int min, max;

    public A(int min_arg, int max_arg)
    {
        min = min_arg;
        max = max_arg;
    }

    public int getMin()
    {
        return this.min;
    }
    public int getMax()
    {
        return this.max;
    }
}

public class B
{
    private final boolean[] myBoolean;
    private final A testObject;

    public B(A testObject)
    {
        // I want to do something like this:
        for(int i = testObject.min(); i != testObject.max(); i++)
        {
            myBoolean[i] = false;
        }
    }
}

I want to initialize the Array "myBoolean" in the class B and set all Values to false. The problem is, that it won't let me do it. All it says is that the blank final field "myBoolean" may not be initialized. I've seen that there's a function called Arrays.fill(array, true/false); but that doesn't help me either. Does someone know a way to initialize the Array myBoolean with the min->max from class A? I know that

private final boolean[] myBoolean;

has no size, the size should be the min->max from the class A object. I just can't find a way to do that.

D idsea J
  • 91
  • 1
  • 7

2 Answers2

2

You need to initialize the array first,

myBoolean = new boolean[size];

Additionally, values within boolean arrays are set to false by default. See here

Mark
  • 46
  • 1
  • 8
Subir Kumar Sao
  • 7,480
  • 3
  • 21
  • 44
0

You have to create an array. You have just declared a reference.

myBoolean = new boolean[100];

Or some value N in place of 100.

Tharun
  • 301
  • 2
  • 13