tags:

views:

157

answers:

2

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;

+1  A: 

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.

TofuBeer
thanks a lot...will newJoe.calculate() get executed in client side JVM or server side JVM?
If you deserialize the XML on the client then it will execute in the client. If you deserialize the XML on the server then it will execute on the server.
TofuBeer
A: 

Yes.

And I'm flattered to be referenced in the example.