dimanche 15 mars 2020

Check if a field is true or false with reflections in java

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




Aucun commentaire:

Enregistrer un commentaire