Reputation:
Given a class like this:
public class Person {
private String firstname;
private String lastname;
private PhoneNumber phone;
private PhoneNumber fax;
// ... constructors and methods
private void calculate()
{
}
}
And an Xstream object like this:
XStream xstream = new XStream(new DomDriver());
Person joe = new Person("Joe", "Walnes");
joe.setPhone(new PhoneNumber(123, "1234-456"));
joe.setFax(new PhoneNumber(123, "9999-999"));
String xml = xstream.toXML(joe);
The resulting XML looks like this:
<person>
<firstname>Joe</firstname>
<lastname>Walnes</lastname>
<phone>
<code>123</code>
<number>1234-456</number>
</phone>
<fax>
<code>123</code>
<number>9999-999</number>
</fax>
</person>
Deserializing an object back from XML looks like this:
Person newJoe = (Person)xstream.fromXML(xml);
After the Person is deserialized is it possible to execute newJoe.calculate() method?
Can the value of the number present in person class be changed to another one like newJoe.number = 4545;
Upvotes: 0
Views: 448
Reputation: 61516
Yes, you can call methods and change the values. It is just like any other instance, the difference is that it got the values from the XML file instead of you explicitly passing them to a constructor.
The object will exist in the VM that you deserialized it. If you want to have this work over the wire you need to use something like RMI to pass the objects around the network.
Upvotes: 1