Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hashCode implementation for singleton class

Tags:

java

what will be the implementation for

public int hashCode()
{
}

method in singleton class? Please do provide me the implementation

like image 680
jezhilvalan Avatar asked Jul 18 '26 00:07

jezhilvalan


2 Answers

Since there's only one object, you don't have to worry about making other, equal, objects have the same hashCode. So you can just use System.identityHashCode (i.e. the default).

like image 120
Matthew Flaschen Avatar answered Jul 19 '26 15:07

Matthew Flaschen


If it's a singleton, you don't need to provide an implementation, as it will be the same object instance wherever it's used. The default (System.identityHashCode(obj)) will be sufficient, or even just a constant number (eg 5)

like image 40
thecoop Avatar answered Jul 19 '26 15:07

thecoop



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!