Domanda

I am wondering what - if this is even possible - the method of determining the access level of a field (private/protected/public) in java.

What I want to do is find out if there is any protected fields in a class. This is the method I have so far in my base abstract class:

public boolean hasOptions() {
    for (Field field : getClass().getDeclaredFields()) {
        // Method to determine if field is protected.
        return true;
    }
    return false;
}

I've looked around without being able to find an answer.

Thanks!

È stato utile?

Soluzione

Yes, use the static utility methods of the Modifier class. For example

Modifier.isProtected(field.getModifiers());
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top