Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if object is from certain package

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.

like image 341
becks Avatar asked Feb 21 '13 15:02

becks


People also ask

How do you check if an object is from a certain class?

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.

How do you check if an object is an instance of another object?

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 .

How do you identify a class in Java?

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().


2 Answers

You can check using

myInstance.getClass().getPackage()
like image 195
Reimeus Avatar answered Sep 20 '22 13:09

Reimeus


if (instanceofClass.getClass().getPackage().getName().equals("packageyouwanttocheck")) {
    // your code
}
like image 26
nsgulliver Avatar answered Sep 19 '22 13:09

nsgulliver