Compare instance of a class to another
I have an incoming object of an unknown class (null possible). I need to test if the object is of a specific class.
Code Java:
if (incomingObject.getClass() == map[0][0].getClass()) {
// go go go!
}
else if(incomingObject.getClass() == null) {
// brakes, turn, go
}
else {
// Ebrake!!
}
While this is functional, it seems bulky to fetch an item from map, and then it's class. Is there a better way to test against a known class without comparing to an existing actual instance of the class? (note this is not to compare the actual objects, but the class itself before acting on the incomingObject) ...maybe there is a better approach from the beginning
Re: Compare instance of a class to another
instanceof operator may be what you are looking for in the short term. Looks like it could be simplified using several different methods/patterns, the Template Pattern being the first that comes to mind, but you could use others or a combination of patterns (for example a Map valued with a class/interface that implements the behavior)
Re: Compare instance of a class to another
Thanks for the instancoof suggestion. By it's name, I thought it would work, but seems this is not so.
Code Java:
class Parent {
public Parent() {
}
}
class Child extends Parent {
public Child() {
super();
}
}
public class MainClass {
public static void main(String[] a) {
Child child = new Child();
if (child instanceof Parent) {
System.out.println("true");
}
}
}
This example I found suggests that MY implementation will always be true because my test is to determine which child of the super was received. Thanks again for the input, while this did not directly solve the problem, it has placed me on the path with a light at the end.
I also found this page which may be useful to others reading this with similar questions.
Re: Compare instance of a class to another
You can get the name of object using reflection.
Code Java:
class Parent {
public Parent() {
}
}
class Child extends Parent {
public Child() {
super();
}
}
public class MainClass {
public static void main(String[] a) {
Child child = new Child();
if (child.getClass().getSimpleName().equals("Child")) {
System.out.println("is child");
}
if (child.getClass().getSimpleName().equals("Parent")) {
System.out.println("is parent");
}
}
}
edit: better yet, you don't even need the name. Just use the class object returned from getClass() and compare that to the class you want (you'll have to first check for null objects, by definition null means the object is every type and no type all at the same time).
Code Java:
class Parent {
public Parent() {
}
}
class Child extends Parent {
public Child() {
super();
}
}
public class MainClass {
public static void main(String[] a) {
Child child = new Child();
if (child.getClass() == Child.class ) {
System.out.println("is child");
}
if (child.getClass() == Parent.class) {
System.out.println("is parent");
}
}
}
Re: Compare instance of a class to another
Quote:
Originally Posted by
helloworld922
edit: better yet...
Code Java:
if (child.getClass() == Parent.class)
So the short answer is use .class for the known, existing object?
So for my use it may look something like this:
Code Java:
if (incomingObject.getClass() == map[0][0].class()) {
// Note the null check is actually before here,
// and this is the final check before the guts run
// so by now its fairly well expected to match class
// but it is known on rare occasion it can be one other class
and if so that leads me to ask what would be wrong with:
Code Java:
if(incomingObject.class == map[0][0].class) {
...since by now the incoming/unknownObject is an instance of something
What is the difference in the two ways? .class vs .getClass()
As it turns out java.util.Iterator answered this problem by elimination, but the question remains.
Re: Compare instance of a class to another
You can only use the .class on actual type names, not variables. If you want to get the same class object from an actual object, use the getClass() method. Note that you will need to check to see if the object is null first since a method call on a null object will always fail.
Code Java:
// this will fail if either incommingObject or map[0][0] are null, you'll need to check for null before-hand
if(incomingObject.getClass() == map[0][0].getClass()) {