Saravanan
Saravanan

Reputation: 43

Pass by Value and reference Java

I am trying to write a copy method which can be used to copy a bean to bean by mapping the property names. if the source object is a cloneable object m cloning it and trying to return it to the calling method. but in calling method it change made in the called method is not reflecting

you might understand if u look at the below code

public class Main {
    public static void main(String[] args) {

        HashMap<Object, Object> source = new HashMap<Object, Object>();
        source.put("test1", "test1");
        source.put("test2", "test2");
        source.put("test3", "test3");

        HashMap<Object, Object> destination = new HashMap<Object, Object>();
        Main m = new Main();

        Main.beanCopy(source, destination);
        System.out.println("caller - " + destination);


    }

    public static void beanCopy(Object source, Object destination) {
        if (source.getClass() == destination.getClass()
                && Cloneable.class.isInstance(source)) {
            Class<? extends Object> cl = source.getClass();
            try {
                Method method = cl.getDeclaredMethod("clone");
                destination = method.invoke(source);
                System.out.println("callee - " +  destination);             
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}

the output of this program is

callee - {test1=test1, test2=test2, test3=test3}
caller - {}

I hope someone could help me to resolve this problem.. Thanks in advance...

Upvotes: 1

Views: 1330

Answers (4)

Oswald
Oswald

Reputation: 31647

In beanCopy, the destination variable holds a pointer to an Object. By assigning

destination = method.invoke(source);

that variable now holds a pointer to a different object.

Java is Pass-by-Value, Dammit!

Upvotes: 0

dty
dty

Reputation: 18998

If, instead of writing that insanely obfuscated code, you did this:

public static void beanCopy(Cloneable source, Object destination) {
    if (source.getClass() == destination.getClass() {
        destination = source.clone();
        System.out.println("callee - " +  destination);             
    }
}

Then the problem might be clearer.

Upvotes: 0

Peter Lawrey
Peter Lawrey

Reputation: 533510

Write the method as a function.

Object destination = Main.beanCopy(source);


public static Object beanCopy(Object source) {

    return destination;
}

The problem you have with your approach is that you have to create an instance of the desired type so you know what to copy. If you want to pass the desired type as well.

MyType destination = Main.beanCopy(source, MyType.class);


public static <T> T beanCopy(Object source, Class<T> clazz) {
    T destination;

    return destination;
}

Note: Java does NOT support pass by reference. ;)

Upvotes: 1

Brian Roach
Brian Roach

Reputation: 76898

You can't change what destination refers to outside the scope of your method.

You would need to return the new object from your method.

Upvotes: 0

Related Questions