hashCode question
Hi,
·
This is more a Java question.
·
When I compile the attached program I get the following error from the compiler: “Error: No method hashCode was found in testExt$extended”
·
My question is why? Why do I get error if supposedly extended should inherit hashCode from Object.
·
This is more a Java question.
·
When I compile the attached program I get the following error from the compiler: “Error: No method hashCode was found in testExt$extended”
·
My question is why? Why do I get error if supposedly extended should inherit hashCode from Object.
java
![](/plugins/FileUpload/images/file.png)
384B
Comments
Object has no hashCode component.
regards peter
shows
· /**
·· * Returns a hash code for this object. This is useful for implementing
·· * hash tables and other data-structures requiring a random index. The
·· * hash code is unique for each object while a program is running.
·· *
·· * @return a unique hash code for this object.
·· */
· //public int hashCode() {
· //· /* ?? This should be implemented to return the pointer to this. */
· //· return 1;
· //}
so apparently hashCode() is not implemented.
You could implement it as
public Object hashCode() {
· return this;
}
but then hashCode is not of type int.
regards peter
Enrique
Unfortunately you cannot use (int)this
but you can compare Objects to each other,
and create arrays of Object.
regards peter
Enrique
Object obj[noparse]/noparse = new Object[noparse][[/noparse]12];
You can store·Objects into this array
myType1 tp1 = new myType1();
myType2 tp2 = new myType2();
etc.
obj[noparse][[/noparse]0] = tp1;
obj[noparse][[/noparse]6] = tp2;
obj[noparse][[/noparse]3] = null; //empty entry
You can then use the index of·obj as a printable value.
As each object has·a unique address (represented by this),
as long as you only allow a specific object to be in obj[noparse]/noparse only once,
the index of obj[noparse]/noparse is also unique·to a specific object.
The index value can be printed or used as index·in an array (of Strings)
holding the names of the objects or whatever.
regards peter