java.lang.StackOverflowError

Discussion in 'Java' started by Asad Khan, Oct 27, 2003.

  1. Asad Khan

    Asad Khan Guest

    I have the following method inside I class,

    public boolean equals(Object o) {
    return (this.equals(o));
    }

    but when i call this method by "foo.equals(bum)" where foo and bum are some
    objects, I get a java.lang.StackOverflowError??

    I kinda know why its happening; I think it calls the same equals method and
    keep going round and round. But, this is what I have to do, without renaming
    the method, so what's the way around it?

    Thanks.
     
    Asad Khan, Oct 27, 2003
    #1
    1. Advertisements

  2. Asad Khan wrote:

    > I have the following method inside I class,
    >
    > public boolean equals(Object o) {
    > return (this.equals(o));
    > }
    >
    > but when i call this method by "foo.equals(bum)" where foo and bum are some
    > objects, I get a java.lang.StackOverflowError??
    >
    > I kinda know why its happening; I think it calls the same equals method and
    > keep going round and round. But, this is what I have to do, without
    > renaming the method, so what's the way around it?
    >
    > Thanks.


    Ofcourse it calls the same equals method. What else would you expect?
    But why do you think that this is what you have to do? Do you have some other
    implementation of equals?

    If you also have something like a

    public boolean equals(SUBCLASS_OF_OBJECT o)

    method in your class, you can call it by being more specific in the call you
    make in your equals method by casting the argument:

    public boolean equals(Object o) {
    return (this.equals((SUBCLASS_OF_OBJECT)o));
    }

    But it is poor form to do things like this. You should only write one equals
    method, with the correct signature.

    --
    Regards,
    Christophe Vanfleteren
     
    Christophe Vanfleteren, Oct 27, 2003
    #2
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Will
    Replies:
    7
    Views:
    16,848
    Adam Jenkins
    Jan 14, 2004
  2. Harald Kirsch
    Replies:
    4
    Views:
    3,093
    Harald Kirsch
    Aug 31, 2004
  3. JPractitioner
    Replies:
    13
    Views:
    20,615
    Roedy Green
    Feb 24, 2006
  4. Replies:
    1
    Views:
    2,626
    Irmen de Jong
    Jun 22, 2004
  5. Replies:
    0
    Views:
    5,010
  6. Replies:
    14
    Views:
    5,581
    Oliver Wong
    Jan 26, 2007
  7. TsanChung
    Replies:
    1
    Views:
    4,204
    Mark Space
    Aug 21, 2008
  8. Replies:
    13
    Views:
    2,546
    Stuart
    Sep 23, 2012
Loading...