when An Array of Objects is declared, how can each object know which array number it has?

I am really scratching my head about this, this should be easy, but I never saw any explanation how it should be done. AT the moment I do it by giving the class in question its own number. Now I have again the same problem and want to find a better solution.

Tagged:

Answers

  • At the moment I am trying things like;

    int numof = this.tinyButton[] ;

    which does not work, it gives "unexpected token ["

  • edited April 2014 Answer ✓

    Objects aren't "aware" about which variable or structure happen to be referencing them!

    PVector p = new PVector(width, height);
    PVector v = p;
    p = null;
    
    • In the 1st statement above, keyword new instantiates a PVector object.
    • Right afterwards, its reference is store in variable p.
    • At next line, variable v also receives that same reference value from p.
    • At last line, variable p stops referencing any object. Only v tracks it from now on.
    • However, the PVector object itself isn't aware that those 2 variables hold its reference in any moment!
    • Neither if any of them stop referencing it! ^#(^
  • Perhaps you should take a look at Processing's references about arrays too:
    http://processing.org/reference/Array.html
    http://processing.org/reference/arrayaccess.html

  • Crap!, so as I understand its mathematically impossible for an object to know where it came from 8-X So the only way for an object to get to know its own name/number is to spoon fed it, is that right?

    Anyway, thx for the answer :) I guess I know know what to do ~O)

  • Answer ✓

    Objects got their own identity, which is their unique reference this.
    That is, every time a new object is instantiated, its own this is instantiated too!

    Also, a hashCode() is generated:

    PVector p = new PVector(width, height);
    println(p.hashCode());
    exit();
    
  • Okay, I solved with this; tinyPush[i].update(i);

    Where the second "i" is the array number of the class

    very easy to do in retrospect.

  • edited April 2014

    Your third answer is intriguing, how do you use the hashCode number? Its use seems to be somewhat abstract this way. :-B

  • edited April 2014

    Where the second "i" is the array number of the class.

    That i variable represents some index of an array object referenced by tinyPush variable.
    Which in turn is completely independent of what value is stored in a particular index!

    If a reference is stored in a structure, the object that reference points to isn't aware of that!
    The index belongs to the structure alone. It's not a property of the stored reference! (~~)

  • edited April 2014 Answer ✓

    Your 3rd answer is intriguing... How do we use the hashCode() value?

    There are some complex structures which care about that plus equals(), like the HashMap for example:
    http://processing.org/reference/HashMap.html
    http://docs.oracle.com/javase/8/docs/api/java/util/Map.html

  • Well, it works now, my problem is/was like a house, the house has a number, but is not capable knowing its own number, it only can if somebody tells it. So that is what I did.

    But your explanation really gives me a bigger sense of the architecture of a language. :)>-

  • "its mathematically impossible for an object to know where it came from"
    Yes. Even more as the same object can be referenced in several variables and arrays.

    PVector v = new PVector(5, 5);
    PVector[] a1 = new PVector[2];
    a1[0] = v;
    PVector[] a2 = new PVector[2];
    a2[1] = v;
    PVector pv = v;
    
    println(v, pv, a1[0], a2[1]);
    
    pv.x = 1;
    
    println(v, pv, a1[0], a2[1]);
    
Sign In or Register to comment.