Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Compare instance of a class to another

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question 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.
    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


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default 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)
    Last edited by copeg; November 26th, 2010 at 12:16 AM.

  3. The Following User Says Thank You to copeg For This Useful Post:

    srs (November 26th, 2010)

  4. #3
    Junior Member
    Join Date
    Nov 2010
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Thumbs up 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.
    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.
    Last edited by srs; November 26th, 2010 at 01:59 AM. Reason: Link added

  5. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Compare instance of a class to another

    You can get the name of object using reflection.

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

    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");
        }
      }
    }
    Last edited by helloworld922; November 26th, 2010 at 05:51 PM.

  6. #5
    Junior Member
    Join Date
    Nov 2010
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Compare instance of a class to another

    Quote Originally Posted by helloworld922 View Post
    edit: better yet...
        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:
    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:
    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.

  7. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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.

    // 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()) {

Similar Threads

  1. Compare method
    By Evelina in forum What's Wrong With My Code?
    Replies: 9
    Last Post: October 16th, 2010, 02:07 PM
  2. Trying to somehow Compare Generics
    By Omega_ryan in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 14th, 2010, 12:58 PM
  3. how to compare two set values
    By humdinger in forum Collections and Generics
    Replies: 1
    Last Post: March 13th, 2010, 11:46 AM
  4. String + Compare // Might be too easy for ya
    By Jangan in forum Java Theory & Questions
    Replies: 1
    Last Post: October 18th, 2009, 05:40 PM
  5. class constants instance constants..... etc
    By chronoz13 in forum Java Theory & Questions
    Replies: 1
    Last Post: August 18th, 2009, 03:38 PM