Reputation: 3024
public class Employee implements java.io.Serializable
{
public String name = "Tom";
public void mailCheck()
{
String address = SomeClass.ItsStaticField(name); //call to static
System.out.println("Mailing a check to " + name
+ " " + address);
}
}
public class SomeClass()
{
// Static Map of <Name, Address>
private static Map<String, String> NameAddressMap= new HashMap<String, String>();
string address;
Static ItsStaticField(name)
{
if (NameAddressMap.containsKey(name){
address = NameAddressMap.get(name);
}
else{
// address =...make a webservice call ... get address for name
NameAddressMap.put(name, address);
}
return address;
}
}
What happens when an object of the above is serialized? And deserialized on another node in a distributed environment? When is 'SomeClass.ItsStaticField()' invoked? Is the 'address' field's value calculated before serialization and then bundled into bytes as part of the serialization process?
Update: Apologize for taking the liberty to add some more info. Added sample code for 'SomeClass'. I have the above code in a distributed environment (specifically Hadoop). I understand this is a bad thing because there are going to be multiple web-service calls, and the intended 'caching mechanism' doesn't really work as expected. What I want to understand is, when and how many times the web-service call would be invoked? And how many instances of the static map 'NameAddressMap' is created on all nodes and when? Thanks much!
Upvotes: 0
Views: 87
Reputation: 3230
When a class is serialized, the node deserializing it need all the .class files required for the deserialization. In your case, the class SomeClass
is part of the dependencies of Employee
. So, in order to deserialize an Employee, both Employee.class and SomeClass.class are required on the local node.
Note: due to Java being dynamically compiled, it should work until you actually try to call the mailCheck
method.
Upvotes: 1
Reputation: 59210
Serialization serialises the objects fields, not its methods. The contents of the methods remain in the class definition, not in the instance itself.
Upvotes: 1
Reputation: 533820
What happens when an object of the above is serialized?
The name
is serialized
And deserialized on another node in a distributed environment?
The name
is deserialized
When is 'SomeClass.ItsStaticField()' invoked?
When you call mailCheck();
Is the 'address' field's value calculated before serialization and then bundled into bytes as part of the serialization process?
No, it is not a member of the object serialized.
Upvotes: 4