How to implement modifier determination in Java
It is believed that many inexperienced people are at a loss about how to realize the modifier judgment in Java. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
There is a class called Modifier in the Java reflection package. There are many judgments about whether public,private and so on.
For example, determine whether the modifier is public
Public static boolean isPublic (int mod) {
Return (mod & PUBLIC)! = 0
}
This int mod exists in Field,Method,Constructor, for example, I have a Field
Public String name
You can pass
Int modifiers = field.getModifiers ()
To get, if I want to determine whether the modifier of this field is public, just use Modifier.isPublic (modifiers)
So how do you know if it is Public by (mod & PUBLIC)! = 0?
explain
PUBLIC is a constant, hexadecimal is 0x00000001, binary is 1
/ * *
* The {@ code int} value representing the {@ code public}
* modifier.
, /
Public static final int PUBLIC = 0x00000001
Mod is an integer. The decimal system of public is 1, and the conversion to binary is also 1.
The concept of and:
Binary and, if both sides are 1, 1 is 1, otherwise 0
Therefore, it is not 0 only if both binaries are the same.
So why use and? can't you use = =?
Yesterday, someone discussed this problem, how to optimize a = b
After reading the above, have you mastered the method of how to implement modifier judgment in Java? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!