I am getting the following error,"The static field IntegerSet.a should be accessed in a static way"

`public static IntegerSet union(IntegerSet setA, IntegerSet setB){
        IntegerSet u = new IntegerSet();
        for (int i=0;i <= CAPACITY; i++)
            if (a[i]==true || setB.a[i]==true)//
        return u;

My Class is as followed:

   public class IntegerSet {
    private static boolean[] a;
    static final int CAPACITY=100;

My complier is saying that the .a[i] in seB.a[i] and u.a[i] are giving me problem. Read a little about the problem , the solution is to put class instead of the varible since its static but how?

Recommended Answers

All 5 Replies

Please post the full text of the compiler's error messages.

put class instead of the varible since its static but how?

Your title shows how: IntegerSet.a

The usages: setA.a, setB.a and u.a will all refer to the SAME variable a since it is static.

The real problem here is not how you are accessing static variables; it's the fact that you are mis-using static in the first place.
static means that every instance of that class shares the one same value - in this case there is only oneboolean[] a, which is shared by all instances of IntegerSet.
That is not what you need. If you have two instances of IntegerSet then each should have its own boolean array. The array and its associated capacity variable should not be static.

since a[i] is a boolean value, the ==true is totally redundant. you can just say

NormR1. -the complier does comply but highlight the following when my cursor is on it

"The static field IntegerSet.a should be accessed in a static way."

JamesCherril - for simplicity i did not post my whole code. if i did u will see i use a[i] in the various methods.
Besides the question did ask me to declare a static variable.

just because your code compiles, doesn't mean it makes sense.
take this code, for instance (snippet of your code)

if ( a[i] == true || setB.a[i] == true )
  u.a[i] = true;

what exactly do you hope to accomplish there?
I assume you noticed this, but a is a static variable. this means, that unlike instance variables, this variable does not have a different value for each instance.
the two main characteristics of static variables' values:
1. you can use them without/before instantiating the class.
2. the variable is class level, not instance level, meaning: if you change the value for the class, or for an instance, you are changing the value for each and every instance.


if ( a[i] == true ) => this automatically means, both setB.a[i] and u.a[i] are also/already true.
if (a[i] == false ) => this automatically means both setB.a[i] and u.a[i] are also/already false.

and, as you have been told, to check whether or not a boolean is true or false, you could just as easily use:

if ( a[i] )


if ( !a[i] )

it would be a lot easier if you showed the entire code and entire error message, btw. are you sure that method is where your error is?

Please post the part where your question requires you to use a static boolean array. A variable does not have to be static for you to use it in multiple methods.
I may be mistaken, but I remain convinced that making that array static is obviously totally wrong, and makes a complete nonsense of any method that involves two IntegerSets (eg your union method).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.