Check if a field is true or false with reflections

Currently trying to code some menu functionality and wondered if there’s a way to find out if a boolean field is true or false. This is the code I’m currently trying but I get an error

try{
      
         field = a.getClass().getField(b); 
        
         if(toggle==1&&field){
         }else if(toggle==1&&!field){
           field.set(a, true);
         }else if(toggle==0&&!field){
         }else if(toggle==0&&field){
           field.set(a, false);
         }
    }catch (NullPointerException e) {
    }catch (NoSuchFieldException e) {
    }catch (IllegalAccessException e) {
    }

the error is

The operator && is undefined for the argument type(s) boolean, Field
1 Like

The datatype Field isn’t itself a boolean obviously.

You need to access it via its method getBoolean():
Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/lang/reflect/Field.html#getBoolean(java.lang.Object)

2 Likes