0

I would like to create a void method that could change the value of the given parameter. I Tried to modify it in the method but it didn't work because it's modifying a copy of the variable.

I Know I can do it in c++ as following :

void Foo::myMethod(MyObject& obj);

I know I can use the return statement in my methods but I would Like to know if there's a way to do as above in java and here is what I tried :

public class LocalCallbackTest {

public static class MyClass {
    private MyClass oldClass = null;

    private MyClass newClass = null;

    private int value = 0;

    public MyClass() {

    }

    public MyClass(MyClass old) {
        this();
        oldClass = old;
    }

    /**
     * @return the newClass
     */
    public MyClass getNewClass() {
        return newClass;
    }

    /**
     * @return the oldClass
     */
    public MyClass getOldClass() {
        return oldClass;
    }

    /**
     * @return the value
     */
    public int getValue() {
        return value;
    }

    /**
     * @param newClass the newClass to set
     */
    public void setNewClass(MyClass newClass) {
        this.newClass = newClass;
    }

    /**
     * @param oldClass the oldClass to set
     */
    public void setOldClass(MyClass oldClass) {
        this.oldClass = oldClass;
    }

    /**
     * @param value the value to set
     */
    public void setValue(int value) {
        this.value = value;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();

        if (oldClass != null)
            sb.append("old class : \n\t").append(oldClass.getValue()).append("\n");
        if (newClass != null)
            sb.append("new class : \n\t").append(newClass.getValue()).append("\n");
        sb.append("actual class : \n\t").append(value).append("\n");

        return sb.toString();
    }

    private MyClass rollBack() {
        if (this.oldClass != null) {
            this.oldClass.setNewClass(this);

            return oldClass;
        }

        return this;
    }

    private MyClass nextMember() {
        if (this.newClass != null) {
            this.newClass.setOldClass(this);

            return newClass;
        }

        return this;
    }

    public static boolean rollBack(MyClass param) {
        if (param != (param = param.rollBack())) {
            return true;
        }
        return false;
    }

    public static boolean nextMember(MyClass param) {
        if (param != (param = param.nextMember())) {
            return true;
        }
        return false;
    }

    public static void test(int i) {
        i++;
    }
}

public static void main(String[] args) {

    MyClass test = new MyClass();

    test.setValue(0);

    for (int i = 1; i <= 5; i++) {
        MyClass test2 = new MyClass();

        test2.setValue(i);

        test2.setOldClass(test);
        test.setNewClass(test2);

        test = test.getNewClass();

        System.out.println(test.getValue());
    }

    int val=2;

    MyClass.test(val);

    System.out.println(val);

    }

}

thank you for your Help.

midugh
  • 608
  • 5
  • 21
  • 1
    Possible duplicate of [Pass object by reference in Java](https://stackoverflow.com/questions/6044649/pass-object-by-reference-in-java) – Ivar Feb 09 '19 at 11:50
  • ok thanks I'll have a look – midugh Feb 09 '19 at 11:52
  • Maybe this one is even better as you are also using primitives in your example: [How to do the equivalent of pass by reference for primitives in Java](https://stackoverflow.com/questions/5614562/how-to-do-the-equivalent-of-pass-by-reference-for-primitives-in-java) – Ivar Feb 09 '19 at 11:53

1 Answers1

0

In C++ you can pass parameters either by copy or by reference (with the &), but in Java, you can only pass an object as a reference, so any object you pass as a parameter will be modified exactly how you want it without having to return it. (like with an & in C++) if it does not work in your example, it's probably because of some logic.

CharlieNoodles
  • 316
  • 2
  • 9