Object Instance as memory

edited October 2013 in How To...

Hi everyone,

Most of us know how to declare a class, and make instances. Now what I want to know is whether it is possible to access an instance's information after it's been made. If it's possible, how do you do this?

-- MenteCode.

P.S. If you're wondering why I need this, its for a robotics simulation.

Answers

  • edited October 2013

    From what I see here, is that getClass() will return the class' general information, rather than info an instance has been assigned, so I don't think this is quite what I'm looking for, but close.

    Not sure if this helps, but I need to access every instance currently existing, gather each of their individual parameters, then display it on screen in graphical form. I'm focusing on the instance access.

    -- MenteCode

  • edited October 2013

    ... info an instance has been assigned, ...

    If that Class class doesn't have what kinda "info" you're looking for, got no idea what that is! 8-X
    Anyways, if we wanna track an object, we have to store its reference at the moment of its instantiation! :-w

  • Answer ✓

    Moved from "Question about Code" to "How To", because I don't see any code in your message... Please, go to Categories and read the descriptions, thanks.

    // Class declaration
    class A { int x; }
    
    // An instance
    A a = new A();
    
    // Write data in the instance:
    a.x = 5;
    
    // "access an instance's information after it's been made"
    println(a.x);
    

    If that's not what you mean, you have to be more precise.

    See also the Objects tutorial.

  • edited October 2013

    ... That's so simple, I can't believe I haven't thought of it. Yes, that's what I was looking for.

    One more thing to clear up, as the Objects tutorial doesn't say this directly: There's no way to have two instances of the same name, correct?

    -- MenteCode

  • There's no way to have two instances of the same class, correct?

    The goal of OOP is to be able to instantiate a class as many times we want/need!
    An object is an instantiated class. And it involves dynamically allocate a memory block and get its pointer reference.

  • Oh, oops. I goofed. I meant to say two instances of the same name.

  • edited October 2013

    In the example PhiLho listed, there can't be two instances with the name "a".

  • edited October 2013

    The only safe rule is this: each object has a unique pointer reference (memory address) in the same computer! o->

  • edited October 2013

    Thanks to the both of you. End of Post.

  • a is not the name of this instance! An instance has no real name of its own... a is just the name of a variable having the reference of the instance.

    You can do:

    A a = new A();
    A b = new A();
    

    to have two distinct instances.

    But you can also do:

    A a = new A();
    A b = a;
    

    and then, the new instance is referenced by both a and b.

  • Oh OK. Thanks for clearing that up.

    As for "A b = a;", what's the point of that? I know this question is technically done, but is there a real purpose for this?

    -- MenteCode

  • edited October 2013

    but is there a real purpose for this?

    In the same scope, perhaps not. But look at this small example I've made and its comments: >:)

    Also shared objects are useful for caching objects in local variables for performance reasons! :ar!

    /** 
     * Shared Object Example (v1.04)
     * by  GoToLoop (2013/Oct)
     * 
     * forum.processing.org/two/discussion/351/object-instance-as-memory
     */
    
    Editor ed;
    PImage img;
    
    void setup() {
      size(200, 200);
      frameRate(10);
      imageMode(CENTER);
    
      // instantiates a PImage object:
      img = createImage(150, 150, ARGB); // img points to a PImage.
    
      // pass that PImage reference to an Editor's constructor.
      ed = new Editor(img);
    }
    
    void draw() {
      background( (color) random(#000000) );
      ed.modify(); // modify PImage obj thru' pic.
      image(img, width>>1, height>>1); // display PImage obj. thru' img.
    }
    
    class Editor {
      final PImage pic;
    
      Editor(PImage image) {
        pic = image;  // pic points to the same object as img now.
      }
    
      void modify() { // modifies the shared PImage object:
        for ( color c[] = pic.pixels, i = c.length; i-- != 0; 
          c[i] = (color) random(#000000) );
    
        pic.updatePixels();
      }
    }
    
Sign In or Register to comment.