I'm stuck on a simple thing. I have an array of booleans named "tags." It's important to me to be able to access each element of the array by boolean:
public boolean energetic, calming, happy, sad;
public boolean[] trackTags = {energetic, calming, happy, sad};
I pass in and assign boolean values to the trackTags array (let's say [true, true, true, false]. So, when I call trackTags[0], I get "true." However, when I print "energetic," - which should be the same as trackTags[0], the value is always false. I know booleans initialize false, but when I switch some values in the trackTags Array for "true," shouldn't the named elements change as well?
Second question: what's a good way for me to interact with boolean variable names? Specifically, if I pass in a String[] [happy, sad], and then want to switch only the boolean[] values corresponding to names in my String array, is this feasible? I have no trouble looping through the elements of both arrays, but I obviously can't compare a string to a boolean value.
All in all, is there a better way to interact with boolean names? I'm really confused about this.
EnumSetmaybe?shouldn't the named elements change as well?No, they shouldn't.booleanis a primitive java type. It means that a boolean variable does not store a reference to the boolean value, but the value itself. When you make this:{energetic, calming, happy, sad}you are copying the value of these four boolean variables to the array, but no the reference to them. So, they are different. If you were handling with objects, instead of a primitive type, then the reference would not be lost and your code would work.