java.lang.StackOverflowError

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Asad Khan

    java.lang.StackOverflowError

    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.Stack OverflowError??

    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.




  • Phil...

    #2
    Re: java.lang.Stack OverflowError

    why do you have to do this?

    "Asad Khan" <uoft_cstutor@y ahoo.com> wrote in message
    news:3eZmb.7766 8$h61.48804@new s01.bloor.is.ne t.cable.rogers. com...[color=blue]
    > 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[/color]
    some[color=blue]
    > objects, I get a java.lang.Stack OverflowError??
    >
    > I kinda know why its happening; I think it calls the same equals method[/color]
    and[color=blue]
    > keep going round and round. But, this is what I have to do, without[/color]
    renaming[color=blue]
    > the method, so what's the way around it?
    >
    > Thanks.
    >
    >
    >
    >[/color]


    Comment

    • Asad Khan

      #3
      Re: java.lang.Stack OverflowError

      well that helps!

      i do because just because. (trying something out).


      "Phil..." <[email protected] > wrote in message
      news:Ix%mb.2790 5$275.49290@att bi_s53...[color=blue]
      > why do you have to do this?
      >
      > "Asad Khan" <uoft_cstutor@y ahoo.com> wrote in message
      > news:3eZmb.7766 8$h61.48804@new s01.bloor.is.ne t.cable.rogers. com...[color=green]
      > > 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[/color]
      > some[color=green]
      > > objects, I get a java.lang.Stack OverflowError??
      > >
      > > I kinda know why its happening; I think it calls the same equals method[/color]
      > and[color=green]
      > > keep going round and round. But, this is what I have to do, without[/color]
      > renaming[color=green]
      > > the method, so what's the way around it?
      > >
      > > Thanks.
      > >
      > >
      > >
      > >[/color]
      >
      >[/color]


      Comment

      • SPG

        #4
        Re: java.lang.Stack OverflowError

        OK, This will definitely not work.

        What you are doing is overriding the equals() method in the super class,
        then recalling it, thus entering an infinite loop causing the stack flow
        exception.

        Only override equals() if you want to compare your oblject to another in a
        different way, IE: by an ID or its name etc..

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

        or if you are intent on overriding the equals method and then re-delgating
        back tot he super class try this:

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

        HTH

        Steve
        "Asad Khan" <uoft_cstutor@y ahoo.com> wrote in message
        news:3eZmb.7766 8$h61.48804@new s01.bloor.is.ne t.cable.rogers. com...[color=blue]
        > 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[/color]
        some[color=blue]
        > objects, I get a java.lang.Stack OverflowError??
        >
        > I kinda know why its happening; I think it calls the same equals method[/color]
        and[color=blue]
        > keep going round and round. But, this is what I have to do, without[/color]
        renaming[color=blue]
        > the method, so what's the way around it?
        >
        > Thanks.
        >
        >
        >
        >[/color]


        Comment

        Working...