Pass by Value and reference Java -


i trying write copy method can used copy bean bean mapping property names. if source object cloneable object m cloning , trying return calling method. in calling method change made in called method not reflecting

you might understand if u @ 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 program

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

i hope me resolve problem.. in advance...

write method function.

object destination = main.beancopy(source);   public static object beancopy(object source) {      return destination; } 

the problem have approach have create instance of desired type know copy. if want pass desired type well.

mytype destination = main.beancopy(source, mytype.class);   public static <t> t beancopy(object source, class<t> clazz) {     t destination;      return destination; } 

note: java not support pass reference. ;)


Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -