This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Serializable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Serializable" Watch "Serializable" New topic
Author

Serializable

Vikrama Sanjeeva
Ranch Hand

Joined: Sep 02, 2001
Posts: 756
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!


Count the flowers of your garden, NOT the leafs which falls away!
Prepare IBM Exam 340 by joining http://groups.yahoo.com/group/IBM340Exam/
Jim Hall
Ranch Hand

Joined: Nov 29, 2001
Posts: 162
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

Joined: Sep 02, 2001
Posts: 756
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

Joined: Nov 29, 2001
Posts: 162
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

Joined: Sep 02, 2001
Posts: 756
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

Joined: Nov 29, 2001
Posts: 162
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).]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Serializable