2

Whenever I am trying to print hash-code of class B I am getting above mentioned errors , please tell what is wrong with my code ?

    class B {
        @Override
        public int hashCode(){
        System.out.println(this.hashCode());    
        //return this.hashCode();
        }   
    }
    public class Testing
    {
      public static void main(String[] args)
      {
       /*A a = new A(0, null, 1.20);
       A a1 = new A(1,null,1.20);
       A a3 = null;*/
       B b = new B();
       System.out.println(b.hashCode());
       //System.out.println(a.equals(a1));
       //System.out.println(a.equals(a1));
     }
 }
9
  • 9
    You have an infinite recursion, the hashCode() method call itself. Commented Sep 23, 2015 at 7:16
  • 1
    it is so cool to ask about stack overflow on stackoverflow! Commented Sep 23, 2015 at 7:17
  • Do you mean System.out.println(super.hashCode());? (Also, this doesn't compile because you're not returning a value). Commented Sep 23, 2015 at 7:17
  • can you tell me how can I override it without infinite recursion? Commented Sep 23, 2015 at 7:17
  • @RamChowdary easy: don't call this.hashCode() in your implementation of hashCode. Commented Sep 23, 2015 at 7:18

3 Answers 3

4

You're creating infinite recursion calling hashCode inside hashCode:

class B {
    @Override
    public int hashCode(){//<----------------------┐
    //                                             │ this path is infinite                  
    System.out.println(this.hashCode()); //   <----┘
    //return this.hashCode();
    }   
}

To avoid this:

  1. Do not call hashCode inside itself.
  2. Call superclass hashcode
    NOTE: if, as in this case, class does not have a direct inheritance, it will always call Object hashCode method.

or

  1. code it by yourself.
    NOTE: to override and create a valid hashCode method, you need at least one field in B class.

    @Override
    public int hashCode(){
        // code it, or better, make your IDE code it for you
    }   
    

    }

Sign up to request clarification or add additional context in comments.

Comments

3

Well, it is an endless recursion, therefore you get the StackOverflowError. What you really want to do is probably.

class B {
    @Override
    public int hashCode(){
        System.out.println(super.hashCode());    
        //return super.hashCode();
    }   
}

If you want to override your implementation and print the value you should do it for example like this.

class B {
    private Long id = Long.valueOf(0L);
    private String name = "";

    @Override
    public int hashCode(){
        final int hashCode = 17 * id.hashCode() + 31 * name.hashCode();
        System.out.println(hashCode);    
        return hashCode;
    }   
}

Or if you simply want to print it then do nothing in the hashCode body.

class B {}

public class Testing
{
   public static void main(String[] args)
   {
    B b = new B();
    System.out.println(b.hashCode());
   }
}

8 Comments

Now which class hash-code I will get here super class hash-code ?
Yes, you will because you have no other implementation than the super class' hashCode. If you want to override the implementation then you have to provide it.
My intention was I have to print current class override hash code value in console .
@RamChowdary But you do not have the current override hash as long as you do not implement it. See my updated answer.
java.util.Objects.hash(id, name) is preferable to magic numbers like 17 and 31.
|
1

Do not call this.hashCode() from hashCode() method. it seems that recursive function, which will take you to Stack over flow error.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.