I have a doubt about the use of this function with reflection; it is supposed to change the value of a variable at the runt time, it changes the value on copy, but does not over-ride the original as late-binding effect during the run time.

import java.lang.reflect.*;
import java.io.*;
import java.lang.*;
public class Test_this extends process_this{
static public void main( String[] args )throws IOException
{
try {
Class cls = Class.forName("process_this");

System.out.println("--aletering the value of a pubic field");
Field fld_int = cls.getField("n2");
process_this change_values = new process_this();
System.out.println("\torignal n2 = " + change_values.n2);
//fld.setDouble(change_values, 123.34);
fld_int.setInt(change_values, 56);
System.out.println("\tchanged n2 = " + change_values.n2);
Class partypes[] = new Class[2];
partypes[0] = String.class;
partypes[1] = Integer.TYPE;
Method meth = cls.getMethod( "method_this", partypes);
process_this methobj = new process_this();
Object arglist[] = new Object[2];
arglist[0] = "Hello";
arglist[1] = new Integer(47);
System.out.println( meth.invoke(methobj, arglist));
}
catch (Throwable e) {
System.err.println(e);
}
}
}
class process_this
{
public String str= "manas";
private int n1 = 23;
public int n2 = 34;
public process_this(){ System.out.println("this is a blank constructor");}
public process_this(String str){ System.out.println("this is a string constructor");}
public process_this(String str, int n3){ System.out.println("this is a string integer constructor");}
protected process_this(String str1, String str2){ System.out.println("this is a string string constructor");}
public String method_this(String str2, int n4)throws IOException
{
String st = "received ";
st += str2;
System.out.println("method will do something about n2 : "+ n2);
System.out.println("method will do something later : "+ this.n1 + n2);
System.out.println("values of st and n4 are : "+ st + ":" + n4);
return st;
}
}
 

 

import java.lang.reflect.*;
import java.io.*;
public class Test_this {
public double d = 12.24;
static public void main( String[] args )throws IOException
{
Test_this ttt = new Test_this();
try {
Class cls = Class.forName("Test_this");
Field fld = cls.getField("d");
Test_this f2obj = new Test_this();
System.out.println("d = " + f2obj.d);
fld.setDouble(f2obj, 34.34);
System.out.println("d = " + f2obj.d);
System.out.println("d = " + ttt.d);
}
catch (Throwable e) {
System.err.println(e);
}
Test_this tt = new Test_this();
//Field fld2 = cls2.getField("d");
System.out.println(tt.d);
}
}