Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Serializable

 
Vikrama Sanjeeva
Ranch Hand
Posts: 760
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Following is quoted from Asim Khan's note(URL is available at request)

If a class implements the Serializable interface, then its public and protected instance variables will be read from & writen to the stream automatically when you use ObjectInputStream & ObjectOutputStream.

Here i want to clear two things that, why not private variables.Since they are private therefore there access is not allowed?.Secondly it tells instance variable, this is b/c static variables are shared therefore can't be serialized.
Bye.
Viki.
------------------
Count the flowers of ur garden,NOT the leafs which falls away!
 
Jim Hall
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just created a class with private variables and wrote it out to an ObjectOutputStream. Then I read the object back in with no problems. Can you provide the URL.
 
Vikrama Sanjeeva
Ranch Hand
Posts: 760
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim here it is.
Can U Post UR Code?.
Bye.
Viki.
------------------
Count the flowers of ur garden,NOT the leafs which falls away!
 
Jim Hall
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this:
<code><pre>
import java.awt.*;
import java.io.*;
public class Test
{
public static void main(String args[]) throws Exception
{
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("javaobjects.dat"));
oos.writeObject(new CarObject());
oos.close();
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("javaobjects.dat"));
Object obj = ois.readObject();
if(obj instanceof CarObject)
{
CarObject car = (CarObject)obj;
System.out.println(car.getMake());
System.out.println(car.getModel());
System.out.println(car.getYear());
System.out.println(car.getColor());
}//end if(obj instanceof CarObject)
}
}
class CarObject implements Serializable
{
//instance variables
public String make = "Chevy";
protected String model = "Corvette";
int year = 2000;
private Color color = Color.red;
public String getMake()
{
return make;
}//end public String getMake()
public String getModel()
{
return model;
}//end public String getName()
public int getYear()
{
return year;
}//end public int getYear()
public Color getColor()
{
return color;
}//end public Color getColor()
}//end class CarObject
</pre>
</code>
 
Vikrama Sanjeeva
Ranch Hand
Posts: 760
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok Jim, i have tried a code.It is reading & writing private variables.Then this means that the statement should be this,

If a class implements the Serializable interface, then its public, protected & private instance variables will be read from & writen to the stream automatically when you use ObjectInputStream & ObjectOutputStream.

instead of this,

If a class implements the Serializable interface, then its public and protected instance variables will be read from & writen to the stream automatically when you use ObjectInputStream & ObjectOutputStream.

Bye.
Viki.
------------------
Count the flowers of ur garden,NOT the leafs which falls away!
 
Jim Hall
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't forget default. It should say all instance variable values will be written unless the transient modifier is used. If transient is used, the variable will be written with the default value (i.e. 0, 0.0, '\u0000', false, null).
[This message has been edited by Jim Hall (edited December 18, 2001).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic