开发者

Pass by Value and reference Java

开发者 https://www.devze.com 2023-02-24 11:22 出处:网络
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 callin

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开发者_JAVA百科 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...


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. ;)


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.


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.


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!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号