Can I change member variable type of serializable class?

Suppose a class implements serializable and I serialize an object of the class. After ding so I change internal implementation such that I change data type of one the variable from int to String.. Is it possible to obtain the values from the serialized object while deserialization using readresolve?

Class A implements Serializable{ private int first; private int second; }

after the serialization:

Class A implements Serializable{ private int first; private String second; }

-------------Problems Reply------------

You'd need to implement a custom readObject() method. More info here.

The serialVersionUID would also need to be the same in both versions of the class. If you don't already declare a serialVersionUID then the jvm will calculate it based on the shape of the class. For this to work you would need to explicitly declare the old serialVersionUID in the new version of the class. More info here

Bored. Let's try this (not compiled)...

import java.io.*;

class A implements Serializable {
private static final long serialVersionUID = 0x...L;
private static final ObjectStreamField[] serialPersistentFields = {
new ObjectStreamField("first", int.class),
new ObjectStreamField("second", int.class),
new ObjectStreamField("secondString", String.class),
};
private int first;
private String second;

private void writeObject(ObjectOutputStream out) throws IOException {
ObjectOutputStream.PutField fields = out.putFields();
fields.put("first", first);
int secondInt;
try {
secondInt = Integer.parseInt(second);
} catch (NumberFormatException exc) {
secondInt = -1;
}
fields.put("second", secondInt);
fields.put("secondString", second);
}
private void readObject(
ObjectInputStream in
) throws IOException, ClassNotFoundException {
ObjectInputStream.GetField fields = out.readFields();
fields = fields.get("first", 0);
int secondInt = fields.get("second", 0);
second = (String)field.get("secondString", Integer.toString(secondInt));
}
}

If either of the fields were final, then you'd need readResolve weirdness or worse.

(Almost left the last secondInt as second there. Boo hiss to String.valueOf.)

Category:java Views:0 Time:2019-01-10

Related post

  • Can i declare member variable as const in class of c++?if yes,how? 2009-07-11

    Can i declare member variable as const in class of c++?if yes,how? --------------Solutions------------- You can - you put const in front of the type name. class C { const int x; public: C() : x (5) { } }; You declare it as you would if it wasn't a me

  • Can't change final variable inside AlertDialog 2012-04-03

    I've got the following code (it's not the full code, but the rest doesn't matter). I'm trying to set the boolean "ignoreLength" to either true or false based on what the user picks in an Alertdialog. However, when the code is like this, I get this er

  • Is there any way I can access Private member variable of a class? 2010-02-08

    Is there any way I can access Private member variable of a class? Editing: Not from a member function or friend function but through an instance. --------------Solutions------------- Just cast it around, shift memory and cast back. (didn't compile th

  • Reflection and changing a variables type at runtime? 2010-05-26

    I'm trying to create an object Of a specific type. I've got the following code, but it fails because it can't cast the new table object to the one that is already defined. I need table to start of an IEnumerable type so I can't declare is an object.

  • Why can't i set a member variable of an interface class as follows 2010-02-25

    So i have a interface class class interfaceClass { public: virtual void func1( void ) = 0; virtual void func2( void ) = 0; protected: int m_interfaceVar; } and a class that inherits from it. Why can't i set the member variable of the interface class

  • Can an object of a class be a member variable of some other class? 2012-03-14

    In System.out.println,'out' is a member variable of System class and it's also an instance of PrintStream class.So can anyone please tell me whether out can be a member of System class which is an object of PrintStream class or not?And please highlig

  • How can I change the Task Type to Fixed Duration for multiple tasks simultaenously. 2012-08-09

    How can I change the Task Type to Fixed Duration for multiple tasks simultaenously. I need to change approximately 700 Tasks and when I select them all and attempt to change their task type, all the tasks are not changed. Is there a more efficient me

  • "can't change the data type" error while extending text field value in Access 2010 2014-06-18

    I have an access database with 900K records in it. I realized that there is a text field that is not long enough, and I decided to change the length to 255 from 50 characters in the design view. WHen i saved the table, it prompt me with an error "X d

  • static member variable inside a local class in c++? 2010-01-06

    I know we cannot declare a static member variable inside a local class... but the reason for it is not clear. So please can anybody explain it? Also, why can't we access a non-static variable define inside the function, within which the local class h

  • Passing in member variables of a specified class 2010-02-10

    I want to pass in the name of a member variable. I thought I could do this by template <typename T::*> void SetVal(T::* newval) { }; This obviously doesn't work, but hopefully gets across what I'm trying to do. I want to be able to set a certai

  • Is it possible to get the value of an overridden NON static member variable of a parent class? 2012-01-23

    Is it possible to get the value of an overridden NON static member variable of a parent class? I understand that to get the value of a STATIC member variable you use self::$var1 or ClassName::$var1, but how do you get the value of a NON static member

  • Where to define static const member variables of a template class 2011-06-22

    According to this question's answer: According to the standards you must define i (which is a static const member) outside of the class definition ... but if I do that for static const member variables of a template class which are themselves generic

  • php and cron job questions, can I change one variable in php file from another php file? 2011-06-14

    I have 3 questions that will greatly help me with my project that I am stuck on, after much narrowing down these are the resulted questions arised from solutions: Can I use one php file to change a variable value in another php file, can these values

  • How to change a variable type in C#? 2010-04-05

    I wanted to use something like this: if(x==5) { var mydb= ........ ; } else { var mydb = ........ ; } but it didn't work because I can't declare a variable inside if statement. So I tried to do this: var mydb; if (x==5) { mydb= ............. ; } else

  • Can we have member variables in Interface? 2010-08-20

    I read somewhere that interfaces can have member variables. Static final constants only, can use them without qualification in classes that implement the interface. On the other paw, these unqualified names pollute the namespace. You can use them and

  • Can a static member variable call non-static member functions? 2010-11-09

    I saw the following code: http://sourcemaking.com/design_patterns/singleton/cpp/1 class GlobalClass { private: int m_value; static GlobalClass *s_instance; GlobalClass(int v = 0) { m_value = v; } public: int get_value() { return m_value; } void set_v

  • A method can't access a member variable of the same class (C++) 2014-01-07

    I wrote a short program to illustrate the principles of inheritance for my school project, but I am having a weird problem. Here is my code: (I have omitted all the code that isn't the problem) class Car { protected: double fuelLevel; public: void fu

  • How can I call member variables of a class within a static method? 2009-11-17

    I am using some method to autoload helper files with functions. The only problem I am having now, is how to call the variables in that class. Because I am not instantiating it as an object, $this won't work. But what will? class some_helperclass { va

  • If you can't change a variable's value in Haskell, how do you create data structures? 2009-11-26

    As per the title. I have the following code which creates a binary search tree, but if I want it created and changed dynamically with user input, how would I do that if I can't change the value of a variable in haskell?!? find :: (Ord a) => Node a

  • How can I change the release type programatically? 2012-02-14

    I have some specific code that runs in debug mode and other code that runs on release mode. I'm trying to write some tests to ensure that only code that is supposed to run based on the configuration runs. Essentially, I have a set of interceptors tha

  • Can you change the data type of a customized defect field in Quality Center after it has been created? 2012-02-16

    We are using HP Quality Center and have created several custom defect fields. When they were initially created many of the fields to alpha numeric and now want them to be purely numeric. Is it possible to change the data type of these fields after th

  • Why can an Object member variable not be both final and volatile in Java? 2012-02-17

    If in a class I have a ConcurrentHashMap instance that will be modified and read by multiple threads I might define like this: public class My Class { private volatile ConcurrentHashMap<String,String> myMap = new ConcurrentHashMap<String,Str

  • How can I change my data types without causing a recompile in Haskell? 2012-02-25

    After watching a video of a talk by Bret Victor, I was inspired to write a quick hack that was somewhat similar to a development environment he demonstrated in the talk. Basically the idea is, one has the app running in one window and whenever one sa

  • Can I have a variable type as a input parameter for a stored procedure in MySQL? 2012-03-28

    For example, the parameter could be of INT type, or FLOAT, or DATETIME, etc. --------------Solutions------------- You mean something like @Qty int as your input parameter? every time you have to manually edit the store function to change the data typ

  • Can't change my account type 2013-06-22

    There are 4 user accounts on my computer. Right now I'm signed on as "Administrator". I click on Control Panel, then User Accounts. My Account Name is "Administrator," that's how I signed in. When I click on "Change your account type" I have the choi

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.090 (s). 11 q(s)