How I check if a variable is a "class" reference - java

How do I check if a given variable is a Class (generic type)? I really don't know how to ask the question better, but let's consider this code:
Object[] o = new Object[] { "string", 12, MyClass.class, new MyClass() };
for (int i = 0; i < o.length; i++) {
if (o[i] instanceof String) ... // TRUE for [0]
else if (o[i] instanceof Integer) ... // TRUE for [1]
else if (o[i] instanceof ???) ... // TRUE for [2] ???
else if (o[i] instanceof MyClass) ... // TRUE for [3]
else ...
}
How do I check the type of the third element of the o array?

Use instanceof to test if it's a class (of any type):
if (o[i] instanceof Class)
Use == to test if it's a specific class:
if (o[i] == MyClass.class)
Class literals are effectively static constants, so there's only one of each class literal per JVM.
Note: If there are multiple class loaders in play then the test for == may be false even though it "should" be true if the two instances were loaded using different class loaders. In this case, even o[i].equals(MyClass.class) will be false.

Use like bellow with instanceOf operator
o[i] instanceof Class

MyClass.class is a literal of type Class, so instance of Class should do.

else if (o[i] instanceof Class) ...
Exactly the same way you'd check if it was an Integer or String. If you want to use the generic type parameter, it'll have to be the ? wildcard:
else if (o[i] instanceof Class<?>) ...
Since generic information is erased at runtime, and it doesn't seem you want to test that it's a specific class anyway.

Related

Getting instance of object?

I have a slight problem. I wrote a function that should generate table for a list of objects (it could be Date or custom one or whichever). Parametars of this function are "List list" and "List headings". So, here is the question, why is this line
if (value.getClass().isInstance(Date.class) ...
not executing, even thou when I print value.getClass() is says the: class java.util.Date. Also a question, how to check if "value" is List? Thanks a lot in advance :)
Here is the part of the code:
for (Object o : list) {
List<String> atributes = new ArrayList<String>();
for (java.lang.reflect.Field field :o.getClass().getDeclaredFields()) {
field.setAccessible(true);
Object value = field.get(o);
if (value != null) {
if (value.getClass().isInstance(Date.class)) {
atributes.add(convertDateToString((java.util.Date) value));
}
atributes.add(value.toString());
}
} ...
You're misusing Class#isInstance, which returns true if Date.class is an instance of value.getClass():
Determines if the specified Object is assignment-compatible with the object represented by this Class. This method is the dynamic equivalent of the Java language instanceof operator. The method returns true if the specified Object argument is non-null and can be cast to the reference type represented by this Class object without raising a ClassCastException. It returns false otherwise.
Rather, you want either:
if(value instanceof Date)
or
if(Date.class.isInstance(value))
The correct way to check if an object is an instance of some Class is to use "instanceof" statement.
So, you should be doing this:
if (value instanceof Date) {
atributes.add(convertDateToString((java.util.Date) value));
}
Hope this will help you.
value.getClass().isInstance(Date.class) asks if Date.class which is of type Class is an instance of a class that is assign-compatible with the class of value. That is false unless value is an instance of Class or Object.
Use operator instanceof instead:
if (value instanceof Date) …

How do I check if a given object is an instance of certain class when in Object[] array?

I was trying to find out how to determine which class a given object instantiate when it's in Object[] array. For example:
Object[] array = new Object[]{175, "sss", new Table(), true};
Object obj = array[0]; // hmm... can this be used as an integer or maybe as a string?
Is it even possible?
You can call getClass() to find out the class of a particular object, or you can use instanceof to check a specific type:
if (array[0] instanceof Integer) {
}
Normally having to do a lot of this indicates a weakness in your design though - you should try to avoid needing to do this.
You can try using instanceof or you can try getClass().isAssignableFrom(), whatever fits your needs
You can test whether it is an instance of a pre-known class (and cast it) like this:
if (obj instanceof String) {
String s = (String) obj; //casts the obj now you know it's a String
}
I like to think of this not as making any changes to the object but just as revealing its true character. For example, it's a bit like seeing a person and not knowing what language they speak - the person is still French, or Italian, just that you don't know which yet. The cast (i.e. (String) obj) is you telling the compiler the equivalent of "I know this person speaks French"
Or you can gets its class like this:
Class<?> clazz = obj.getClass();
A Class instance can be used to make the same check:
String.class.isInstance(obj) {
String s = String.class.cast(obj);
}

Is it better to use class.isEnum() or instanceof Enum?

I have an object. I want to check to see if it is of type enum. There are two ways to do this.
object.getClass().isEnum()
or
object instanceof Enum
Is one better?
In my opinion object instanceof Enum is better for several reasons:
It is very obvious what is asked here: "is this an enum"?
It doesn't risk a NullPointerException (if object is null, it will just evaluate to false)
It's shorter.
The only reason I'd see for using isEnum() would be if I only have access to the Class object and not to a concrete instance.
You need to use the latter (object instanceof Enum) because the former may not work with enum constants with constant-specific class bodies.
For example, for this enum type:
enum MyEnum {
FOO { }
}
The expression MyEnum.FOO.getClass().isEnum() returns false.
If you want to check if an object is a enum constant without instanceof Enum, you have to use this (much more complicated) expression:
static boolean isEnum(Object obj) {
Class<?> cls = obj.getClass();
Class<?> superCls = cls.getSuperclass();
// Be careful, Object.class.getSuperclass() returns null
return cls.isEnum() || (superCls != null && superCls.isEnum());
}

Check if an object belongs to a class in Java [duplicate]

This question already has answers here:
How to determine an object's class?
(13 answers)
Closed 9 years ago.
Is there an easy way to verify that an object belongs to a given class? For example, I could do
if(a.getClass() = (new MyClass()).getClass())
{
//do something
}
but this requires instantiating a new object on the fly each time, only to discard it. Is there a better way to check that "a" belongs to the class "MyClass"?
The instanceof keyword, as described by the other answers, is usually what you would want.
Keep in mind that instanceof will return true for superclasses as well.
If you want to see if an object is a direct instance of a class, you could compare the class. You can get the class object of an instance via getClass(). And you can statically access a specific class via ClassName.class.
So for example:
if (a.getClass() == X.class) {
// do something
}
In the above example, the condition is true if a is an instance of X, but not if a is an instance of a subclass of X.
In comparison:
if (a instanceof X) {
// do something
}
In the instanceof example, the condition is true if a is an instance of X, or if a is an instance of a subclass of X.
Most of the time, instanceof is right.
If you ever need to do this dynamically, you can use the following:
boolean isInstance(Object object, Class<?> type) {
return type.isInstance(object);
}
You can get an instance of java.lang.Class by calling the instance method Object::getClass on any object (returns the Class which that object is an instance of), or you can use class literals (for example, String.class, List.class, int[].class). There are other ways as well, through the reflection API (which Class itself is the entry point for).
Use the instanceof operator:
if(a instanceof MyClass)
{
//do something
}
I agree with the use of instanceof already mentioned.
An additional benefit of using instanceof is that when used with a null reference instanceof of will return false, while a.getClass() would throw a NullPointerException.
Try operator instanceof.
The usual way would be:
if (a instanceof A)
However, there are cases when you can't do this, such as when A in a generic argument.
Due to Java's type erasure, the following won't compile:
<A> boolean someMethod(Object a) {
if (a instanceof A)
...
}
and the following won't work (and will produce an unchecked cast warning):
<A> void someMethod(Object a) {
try {
A casted = (A)a;
} catch (ClassCastException e) {
...
}
}
You can't cast to A at runtime, because at runtime, A is essentially Object.
The solutions to such cases is to use a Class instead of the generic argument:
void someMethod(Object a, Class<A> aClass) {
if (aClass.isInstance(a)) {
A casted = aClass.cast(a);
...
}
}
You can then call the method as:
someMethod(myInstance, MyClass.class);
someMethod(myInstance, OtherClass.class);

Is there a way to determine what type a class is an instance of in Java?

Say I have 3 classes like so:
class A {}
class B extends A {}
class C extends A {}
Would it then be possible to determine whether a particular object was an instance of A, B, or C?
I thought that something like this might work:
if (myObject.getClass().isInstance(B.class)) {
// do something for B
} else (myObject.getClass().isInstance(C.class)) {
// do something for C
} else {
// do something for A
}
but after reading a little I think it would always evaluate as B since it's just testing if a cast would work and there's no substantial differences between them.
The simpler and faster code is:
if (myObject instanceof B) {
} else if (myObject instanceof C) {
} else if (myObject instanceof A) {
}
Note that the order is important: you have to have the test for A last, as that will succeed for instances of B and C as well.
However, your original code would nearly work. Class.isInstance checks whether the value really is an instance of the given class or any superclass. So if myObject is an instance of C, then B.class.isInstance(myObject) will return false. All you've got wrong is that you're calling getClass() on myObject unnecessarily, instead of using B.class etc.
This is the approach you would take if you didn't know which classes you were interested in at compile time - the instanceof operator only works when you can specify the type statically in code.
Now if you want to find out whether myObject is an instance of exactly B (and not a subclass) then just use:
if (myObject.getClass() == B.class)
(This will blow up if myObject is a null reference, of course.)
Do this:
if (myObject instanceof B) {
// do something for B
} else (myObject instanceof C) {
// do something for C
} else {
// do something for A
}
You also might want to look at the double dispatch idiom which is an OO way of changing behaviour based on the type of an argument in languages which don't support multi-methods.
There is the instanceof operator that does what you want, as others have answered.
But beware that if you need something like this, it's a sign that there might be a flaw in the design of your program. The better, more object oriented way to do this is by using polymorphism: put a method in the superclass A, override that method in B and C, and call the method on myObject (which should have type A):
A myObject = ...; // wherever you get this from
// Override someMethod in class B and C to do the things that are
// specific to those classes
myObject.someMethod();
instanceof is ugly and should be avoided as much as possible, just like casting is ugly, potentially unsafe and should be avoided.
you could do this
if (myObject.getClass() == B.class) {
// do something for B (not subclasses of b!!!)
} else if(myObject.getClass() == C.class) {
// do something for C (not subclasses of c!!!)
} else if(myobject.getClass() == A.class) {
// do something for A (not subclasses of A!!)
} else if(myobjects instanceof A){
//all other subclasses of A
}

Categories