-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.

3
  • What's the size of your array supposed to be? Commented Nov 16, 2017 at 3:57
  • how about using new and actually creating this array before trying to access it? Commented Nov 16, 2017 at 3:58
  • The arrays size must be the min->max from Object A. Commented Nov 16, 2017 at 4:05

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

3 Comments

Yes. But the array size must be the min to max from class Object A.
You can get the size by size = Max - Min
Thanks Subir, that actually helped to solve my problem.
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.