# Check if a field is true or false with reflections

**URL:** <https://discourse.processing.org/t/check-if-a-field-is-true-or-false-with-reflections/18692>\
**Category:** Coding Questions\
**Created:** [March 16, 2020, 1:03am UTC](https://discourse.processing.org/t/check-if-a-field-is-true-or-false-with-reflections/18692 "2020-03-16T01:03:23Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)\
**Post date:** [March 16, 2020, 1:03am UTC](https://discourse.processing.org/t/check-if-a-field-is-true-or-false-with-reflections/18692/1 "2020-03-16T01:03:23Z")

</div>

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

```auto
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

```auto
The operator && is undefined for the argument type(s) boolean, Field

```

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [March 16, 2020, 7:43am UTC](https://discourse.processing.org/t/check-if-a-field-is-true-or-false-with-reflections/18692/2 "2020-03-16T07:43:32Z")

</div>

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)](http://Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/lang/reflect/Field.html#getBoolean(java.lang.Object))
