How can I update variable value that received from argument (perhaps, something like C++'s pointer)?

I have this code.

http://pastebin.com/HU1agrXJ

What happened now is that when I increase the value of i the value of test.i will always be the same as it was first time assigned.

What I want is that when I update the value of i the value of test.i is also updated.

I was thinking perhaps assigning the address reference instead of the value will solve this problem. But how can I do this in Java/Processing? Is there any alternative rather than assigned the value over time (the Test object will be around ~1000 in amount)?

Tagged:

Answers

  • The code is short enough to show in this forum and it will make it easier for members to discuss it, so here it is

    class Test {
    
      int i = 0;
      int j = 0;
      Test(int _i, int _j) {
    
        i = _i;
        j = _j;
      }
      Test() {
      }
    }
    
    int i = 0;
    int j = 0;
    Test test = new Test(i, j);
    void setup() {
    }
    void draw() {
    
      i ++;
      j ++;
      println(
    
        i + " " + j + " " +
        test.i + " " + test.j
    
        );
    }
    
  • Unfortunately for you its is not possible to link the i in Test to the global i Java does not support C style pointers. Since you want these 2 variables always to have the same value why have them as separate variables?

  • How can I use one variable for several different classes?

    Let say I have a private function named PrivateFunction(int _i){} in Test class. How can I get int _i filled from a value outside of Test class?

    Meta question, how can you show code in this forum?

  • Answer ✓

    If you really want to pass a only a reference, you could create an array of length one. Unlike primitive datatypes, arrays and objects are passed by reference.

    class Test {
      int i[] = {0};
      Test(int[] _i) {
        i = _i;
      }
    }
    
    int i[] = {0};
    Test test = new Test(i);
    void setup() {
    }
    void draw() {
      i[0] ++;
      println(i[0] + " " + test.i[0] );
    }
    

    But i don't have any idea where this could be useful at the moment.

  • edited October 2015 Answer ✓

    Unlike primitive datatypes, arrays and objects are passed by reference.

    Some pedantic observations: :-B

    1. Arrays are also object types but w/o a formal class which represents it. Only thru' [] operator.
    2. Pass-by-reference doesn't mean passing some value which represents any reference.
    3. Rather it's when we pass the reference/pointer of variables themselves. Which Java can't!
    4. In other words, pass-by-reference is the same as passing a variable's own memory address.
  • Answer ✓

    How can I use one variable for several different classes?

    In Processing any class defined inside a pde tab is an inner class so has access to any variables declared in the sketch. The sketch below 2 classes sharing the same variables.

    class TestA {
    
      void printIJ() {
        println("TestA " + i+ "  " + j);
      }
    }
    
    class TestB {
    
      void printIJ() {
        println("TestB " + i+ "  " + j);
      }
    }
    
    
    int i = 0;
    int j = 0;
    TestA a = new TestA();
    TestB b = new TestB();
    
    void setup() {
      frameRate(5); // Just slow the output down
    }
    
    void draw() {
      i ++;
      j ++;
      println("\nGlobal " + i + " " + j + " ");
      a.printIJ();
      b.printIJ();
    }
    

    The output was

    Global 1 1 
    TestA 1  1
    TestB 1  1
    
    Global 2 2 
    TestA 2  2
    TestB 2  2
    
    Global 3 3 
    TestA 3  3
    TestB 3  3
    
    ...
  • Cool! Okay, now if I want to pass primitive data - type I just need to make it a global variable. However, if the data - type is an object it will automatically pass a reference instead of value in Java.

  • edited October 2015

    ... now if I want to pass primitive datatype I just need to make it a global variable.

    1. Java doesn't have "global" variables but fields. Which are also known as properties in JS.
    2. Fields, methods & constructors are class members.
    3. Processing's "sketch" is an instance of class PApplet. More precisely, a subclass of it.
    4. Therefore its "global" variables are in fact PApplet fields.

    ... if the datatype is an object it will automatically pass a reference instead of value in Java.

    1. Even references/pointers are values. Datatype determines how values are interpreted.
    2. Java doesn't allow references of variables being stored nor passed around though.
Sign In or Register to comment.