If class B and class C extend class A and I have an object of type B or C, how can I determine of which type it is an instance?
If class B and class C extend class A and I have an object of type B or C, how can I determine of which type it is an instance?
Use Object.getClass. It returns the runtime type of the object. Here's how to call it using your example:
class A {}
class B extends A {}
class C extends A {}
class Main {
public static void main(String args[]) {
C c = new C();
Class clazz = c.getClass();
System.out.println(clazz);
}
}
Output:
class C
You can also compare two Class instances to see if two objects are the same type.
class A {}
class B extends A {}
class C extends A {}
class Main {
public static void main(String args[]) {
B b = new B();
Class c1 = b.getClass();
C c = new C();
Class c2 = c.getClass();
System.out.println(c1 == c2);
}
}
You can use:
Object instance = new SomeClass();
instance.getClass().getName(); //will return the name (as String) (== "SomeClass")
instance.getClass(); //will return the SomeClass' Class object
HTH. But I think most of the time it is no good practice to use that for control flow or something similar...
Any use of any of the methods suggested is considered a code smell which is based in a bad OO design.
If your design is good, you should not find yourself needing to use getClass() or instanceof.
Any of the suggested methods will do, but just something to keep in mind, design-wise.
There is also an .isInstance method on the "Class" class. if you get an object's class via myBanana.getClass() you can see if your object myApple is an instance of the same class as myBanana via
myBanana.getClass().isInstance(myApple)
checking with isinstance() would not be enough if you want to know in run time.
use:
if(someObject.getClass().equals(C.class){
// do something
}
I Used Java 8 generics to get what is the object instance at runtime rather than having to use switch case
public <T> void print(T data) {
System.out.println(data.getClass().getName()+" => The data is " + data);
}
pass any type of data and the method will print the type of data you passed while calling it. eg
String str = "Hello World";
int number = 10;
double decimal = 10.0;
float f = 10F;
long l = 10L;
List list = new ArrayList();
print(str);
print(number);
print(decimal);
print(f);
print(l);
print(list);
Following is the output
java.lang.String => The data is Hello World
java.lang.Integer => The data is 10
java.lang.Double => The data is 10.0
java.lang.Float => The data is 10.0
java.lang.Long => The data is 10
java.util.ArrayList => The data is []
You can use getSimpleName().
Let's say we have a object: Dog d = new Dog(),
The we can use below statement to get the class name: Dog. E.g.:
d.getClass().getSimpleName(); // return String 'Dog'.
PS: d.getClass() will give you the full name of your object.
I use the blow function in my GeneralUtils class, check it may be useful
public String getFieldType(Object o) {
if (o == null) {
return "Unable to identify the class name";
}
return o.getClass().getName();
}
use instanceof operator to find weather a object is of particular class or not.
booleanValue = (object instanceof class)
JDK 14 extends the instanceof operator: you can specify a binding variable; if the result of the instanceof operator is true, then the object being tested is assigned to the binding variable.
please visit official Java documentation for more reference.
instanceof operator :import java.util.*;
class Foo{
@Override
public String toString(){
return "Bar";
}
}
class Bar{
public Object reference;
@Override
public String toString(){
return "Foo";
}
}
public class InstanceofUsage{
public static void main(final String ... $){
final List<Object> list = new ArrayList<>();
var out = System.out;
list.add(new String("Foo Loves Bar"));
list.add(33.3);
list.add(404_404);
list.add(new Foo());
list.add(new Bar());
for(final var o : list){
if(o instanceof Bar b && b.reference == null){
out.println("Bar : Null");
}
if(o instanceof String s){
out.println("String : "+s);
}
if(o instanceof Foo f){
out.println("Foo : "+f);
}
}
}
}
$ javac InstanceofUsage.java && java InstanceofUsage
String : Foo Loves Bar
Foo : Bar
Bar : Null