Results 1 to 2 of 2
  1. #1
    fultonwilcox is offline Member
    Join Date
    Sep 2012
    Posts
    7
    Rep Power
    0

    Default hash code method not working? StackOverflowError

    So, I do not understand why I cannot define my own public int hashCode() function for my class. I get the error: Exception in thread "main" java.lang.StackOverflowError | when I do. It works when i change it to hashcode()

    Here is the code:

    Java Code:
    
    public class LabeledPoint {
    	private int x;
    	private int y;
    	private String label;
    	
    	public LabeledPoint(int xx, int yy, String aLabel)
    	{
    		x = xx;
    		y = yy;
    		label = aLabel;
    	}
    	
    	/**
    	 * Gives hashcode of object
    	 */
    	public int hashCode()
    	{
    		return this.hashCode();
    	}
    	
    	/**
    	 * Determines if two objects are equal
    	 * @return true if they are,
    	 * 			false if not
    	 */
    	public boolean equals(LabeledPoint lp)
    	{
    		if( x == lp.x )
    			if( y == lp.y )
    				if( label.equals(lp.label) )
    					return true;
    		return false;
    	}
    }
    Java Code:
    
    public class LabeledPointTest {
    	
    	public static void main(String[] args)
    	{
    		LabeledPoint a = new LabeledPoint(1,1, "a");
    		LabeledPoint b = new LabeledPoint(1,1,"b");
    		
    		if( a.equals(b))
    			System.out.println("a == b");
    		else
    			System.out.println("a != b");
    		
    		LabeledPoint c = a;
    		if( c.equals(a) )
    			System.out.println("a == c");
    		else
    			System.out.println("a != c");
    		
    		System.out.println("a hash code: "+ a.hashCode() );
    		System.out.println("b hash code: "+ b.hashCode() );
    		System.out.println("c hash code: "+ c.hashCode() );
    	}
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: hash code method not working? StackOverflowError

    OK, let's look at your hashCode() method and walk through it pretending we're the JVM (a useful exercise to practice and get good at!):

    First, your method:
    Java Code:
        public int hashCode()
        {
            return this.hashCode();
        }
    Next, let's pretend we're the JVM and that this method gets called, what happens:

    Java Code:
    hashCode() gets called
     return method calls hashCode again
      whose return method calls hashCode again
       whose return method calls hashCode again
        whose return method calls hashCode again
         whose return method calls hashCode again
          whose return method calls hashCode again
           whose return method calls hashCode again
            .... etc...
    where will this recursive method calling itself end? The answer -- when the program runs out of stack memory and crashes!

    Solution: don't recursively call the method inside of itself.

Similar Threads

  1. Replies: 1
    Last Post: 03-25-2012, 02:40 AM
  2. Replies: 1
    Last Post: 12-15-2011, 04:02 AM
  3. Replies: 15
    Last Post: 12-11-2011, 09:20 PM
  4. Replies: 0
    Last Post: 08-07-2011, 08:32 PM
  5. Replies: 2
    Last Post: 01-04-2011, 04:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •