Shop OBEX P1 Docs P2 Docs Learn Events
hashCode question — Parallax Forums

hashCode question

EnriqueEnrique Posts: 90
edited 2007-10-24 20:39 in General Discussion
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.

Comments

  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2007-10-24 17:51
    Javelin java is not like pc java.
    Object has no hashCode component.

    regards peter
  • EnriqueEnrique Posts: 90
    edited 2007-10-24 18:22
    But the Javelin Stamp User’s manual on page 112 states that there is a hashCode method.
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2007-10-24 19:23
    The file Object.java in folder ...\lib\java\lang
    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
  • EnriqueEnrique Posts: 90
    edited 2007-10-24 19:29
    What's the type of this?


    Enrique
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2007-10-24 19:59
    The type of this is Object.
    Unfortunately you cannot use (int)this
    but you can compare Objects to each other,
    and create arrays of Object.

    regards peter
  • EnriqueEnrique Posts: 90
    edited 2007-10-24 20:11
    So basically this is a reference to Object. I want to have a way of identifying different instances of object of the same class so I can print debugging information on the terminal and know from whom it’s coming. Any idea how to do it?


    Enrique
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2007-10-24 20:35
    You can create an arrray of Object

    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
  • EnriqueEnrique Posts: 90
    edited 2007-10-24 20:39
    Thanks
Sign In or Register to comment.