How to declare a constant set visible for each instance of a class?

I would like to have a constant set in my class that will be visible to all instances of the class.

Firstly, I don’t know if it should be declared as “static”. As far as I understand, any changes to the static field (made by one of the instances) will be considered by other instances (therefore, the static variable is not tied to a specific instance). Moreover, the static field can be changed without using any instance (we work directly with the class). Thus, all these special properties of a static field are related to how it can be changed and what are the consequences of these changes. But in my case, I would like to have a constant (therefore, problems with "changes" are not relevant here). Therefore, probably I do not need to use "static". Correctly?

Secondly, my set will contain many elements, and I do not want to immediately determine the value of the set (when I create a variable). In other words, I would like to declare a set, and then add the elements to this set step by step. But I can not do this if I work with constants. Is it possible to set a given value and then make it permanent?

Thirdly, I realized that some problems might arise if I try to change the value of variables outside of any method. So how does it work?

ADDED:

OK Thanks to the answer, I realized that it should be “final” and “static” (since it is constant and it will not be associated with any particular instance, it should be visible to all instances of the class). However, I still have a problem. I wanted to specify a set using "add", and I cannot add to a set if it is permanent. Moreover, I cannot change the values ​​of variables outside of methods (why?). Anyway, I do not insist on using "add" to define the set. I am ready to immediately identify it. But I do not know how to do this. I tried things like this:

final static Set allowedParameters = new HashSet("aaa","bbb");
final static Set allowedParameters = new HashSet(["aaa","bbb"]);
final static Set allowedParameters = new HashSet({"aaa","bbb"});
final static Set allowedParameters = new HashSet(Arrays.asList({"userName"}));

And they did not work.

ADDED 2:

Can someone explain to me the requests, the code given by Tadeusz Kopek?

class YourClass {
    private static void fillSet(Set<SomeType> set) {
        // here you add elements, like
        set.add(new SomeType());
    }
    private final static Set<SomeType> yourSetField;
    static {
        final Set<SomeType> tempSet = new HashSet<SomeType>();
        fillSet(tempSet);
        yourSetField = Collection.unmodifiableSet(tempSet);
    }
}


1. fillSet , "set". ?
2. SomeType() fillSet? ?
3. fillSet? . ?
4. tempSet ?
5. unmodifiableSet ? , , . , yourSetField final? .

+3
4

, ? , :

class YourClass {
    private static void fillSet(Set<SomeType> set) {
        // here you add elements, like
        set.add(new SomeType());
    }
    private final static Set<SomeType> yourSetField;
    static {
        final Set<SomeType> tempSet = new HashSet<SomeType>();
        fillSet(tempSet);
        yourSetField = Collections.unmodifiableSet(tempSet);
    }
}

- , . , .

, concurrency - extraneon.


, .

- < > : , Java 1.5 generics. - List, . , , .

List myList = new ArrayList();
myList.add("Hello, my Jon Skeet Number decreases");
String firstElement = (String) myList.get(0);

String. , BigDecimal myList. String, ClassCastException.

myList.add(0, BigDecimal.ZERO); // perfectly legal
String anotherString = (String) myList.get(0); // compiles, but ClassCastException at runtime

, Java 1.5 . , List . < > :

List<String> myList = new ArrayList<String>();
myList.add("Hi everybody");
String firstElem = myList.get(0); // no cast required
myList.add(BigDecimal.ZERO); // compiler error: cannot cast BigDecimal to String

, Sets. , . SomeType , , . , .

- . - :

static private int instanceCount = 0;

, .

static {
    // some code, that can use class static variables, class static methods, declare its own variables etc.
}

, .

  • set fillSet. : set.add(new SomeType());
  • , , SomeType. , . new SomeType(); () SomeType, .
  • fillSet , , - ( ). , fillSet. fillSet - , , . , .
  • tempSet - , . , . , findBugs, , .
  • finalSetField , yourSetField = new HashSet<SomeType>() . , yourSetField, yourSetField.add(...). yourSetField Set, (UnsupportedOperationException). : final , yourSetField ( ). unmodifiableSet , . , .
+6

, , - , , , .

, , (, , Guava) . . .

+4

- :

private static final Set<Foo> mySet;
static {
 // ...initialize contents here.  guava example looks like:
mySet = ImmutableSet.of( adc, 123, etc );

}

Guava ImmutableSet, , of( ... ) ( - - , ()), API. .

+4

, , static. Set, , "", Set , final.

, .

; concurrency. , m, Set ? Set? , Synchronized Set.

, :

private static final Set<YourElement> mySet = Collections.synchronizedSet(new HashSet());

, , , , .

void myFoo() {
  mySet = new HashSet(); // will fail as it final
}

will fail, as expected, and the community set updates will work.

If you need a Set with constant values, you can do:

private static final Set<YourElement> mySet;
static {
   Set<YourElement> tmpSet = new HashSet();
   tmpSet.add(...);
   mySet = Collections.unmodifiableSet(tmpSet);
}

But I see that someone was the first :)

+2
source

Source: https://habr.com/ru/post/1736329/


All Articles