Passing by reference in Java?

后端 未结 4 1342
Happy的楠姐
Happy的楠姐 2020-12-20 22:19

In C++, if you need to have 2 objects modified, you can pass by reference. How do you accomplish this in java? Assume the 2 objects are primitive types such as int.

4条回答
  •  礼貌的吻别
    2020-12-20 23:19

    Using generics you can create a pointer class which would make this at least somewhat less painful. You pass in the object, change it's value property and when the function exits your object will contain the new value.

    MyPointerClass PointerClass = new MyPointerClass(5);
    myFunction(PointerClass);
    System.out.println(PointerClass.Value.toString());
    
    // ...
    
    void myFunction(myPointerClass SomeValue)
    {
      SomeValue.Value = 10;
    }
    

提交回复
热议问题