Making a calling method point to a new object after returning

A

Ads

I need to replace a object a user is using with another object of same type:
ie:


//Here myObjectA is something
myFunction(myObjectA);
//now myObjectA is completly different

public void myFunction(MyObject myObjectA){
//creates a new MyObject using function I don't have access to
somehow set myObjectA to this new object
}

For various reason, we can't simply returning the new object from myFunction
(ie doing myObjectA = myFunction(myObjectA))

So what i've come up with is copying all the fields from one object into the
other:
as a function inside MyObject (using reflection for maintenance reasons):

public void copyInto(MyObject obj){

Field [] fields = Main.class.getDeclaredFields();
try {
for (int i = 0; i < fields.length; i++) {
fields.set(this, fields.get(obj));
System.out.println(fields.getName());
}
}
catch (Exception ex) {
ex.printStackTrace();
}
}

This code seems to work, but does it have any flaws I havn't noticed yet?

Also, there is a field called class$packagename$MyObject , what is this
field?

Any help appreciated

Ads
 
D

David Zimmerman

Also, there is a field called class$packagename$MyObject , what is this
field?
It's put there by the compiler as part of <name>.class

The <name>.class construct is compiler fiction. IE the field doesn't
really exist, but the compiler gets the programmer to beleive that it
does.

Try using 'javap -c' to see the code the compiler generates when the
programmer uses <anme>.class
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top