Is there a way to check if an object's class is from a certain package?
Like, when I check if an object is of a certain class using instanceof
,
I want something similar to check for a package in Java.
The java “instanceof” operator is used to test whether the object is an instance of the specified type (class or subclass or interface). It is also known as type comparison operator because it compares the instance with type. It returns either true or false.
The instanceof operator in Java is used to check whether an object is an instance of a particular class or not. objectName instanceOf className; Here, if objectName is an instance of className , the operator returns true . Otherwise, it returns false .
Example 1: Check the class of an object using getClass() In the above example, we have used the getClass() method of the Object class to get the class name of the objects obj1 and obj2 . To learn more, visit Java Object getClass().
You can check using
myInstance.getClass().getPackage()
if (instanceofClass.getClass().getPackage().getName().equals("packageyouwanttocheck")) {
// your code
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With