aspose file tools*
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

laoniu gougou
Greenhorn

Joined: Jan 25, 2001
Posts: 25
Here is some code:
public class MyTest implements Serializable{
public static void main(String[] args){
MyTest obj = new MyTest();
try{
File f = new File("new.java");
RandomAccessFile rw = new RandomAccessFile(f,"rw");
FileOutputStream f_str = new FileOutputStream(f);
ObjectOutputStream o_str = new ObjectOutputStream(f_str);
o_str.writeObject(obj);
o_str.close();
}
catch(IOException e)
{
System.out.println("Can not create Read only");
}
finally{

}

}
public int s_pub=5;
private int s_private = 6;
static int s_static = 7;
public transient int s_tran = 8;
}

I have some questions about this:
1) Seems to me "private int s_private is also got output to the file new.java. Since it is private, shouldn't it not be able to accessed and output?
2) When I open the new.java, it has some strange character, what is it? Is it ASCII code?
I appreciate!
S Dave
Ranch Hand

Joined: Jan 28, 2001
Posts: 103
private variables cannot be accessed outside the class in which they are declared that means no other objects other than the objects of same class in which they are declared can access them.
when u serialize a class u only write the state of the class onto a file or stream.
transient variables, as u must be aware, are not serialized.
so if u read back the object u wrote on to new.java and print it out u would get MyJava@...some hashcode.
if u try the following:
try{
File f = new File("new.java");
RandomAccessFile rw = new RandomAccessFile(f,"rw");
FileInputStream f_str = new FileInputStream(f);
ObjectInputStream i_str = new ObjectInputStream(f_str);
Test5 robj = (Test5)i_str.readObject();
i_str.close();
System.out.println("Read : "+ robj.s_pub);
System.out.println("Read : "+ robj.s_private);
System.out.println("Read : "+ robj.s_static);
System.out.println("Read : "+ robj.s_tran);
}
catch(Exception e)
{e.printStackTrace();}
u 'll get the variables that u had serialized as follows:
Read : 5 //s_pub
Read : 6 //s_private
Read : 7 //s_static
Read : 0 //s_transient - which never got serialized
Hari Gangadharan
Ranch Hand

Joined: Mar 08, 2001
Posts: 73
Sweekriti has explained it pretty well but I thought I will add some more lines to give it a bit more clarity. The objects serialized are written to file in the form of unicodes. You may view it in many of the platforms since they support Unicode. If you open it in an editor whch will not support Unicode then you will not be able to see it. The funny characters you see may be the integer values -- for example integer 5 will be written as 5 whereas unicode value of character '5' is 53.
It is true and was one of my confusions on why and how the ObjectOutputStream object can access the private variables of your Class?? It kind of violated Java's OO principles! The answer I got is ObjectOutputStream can access the private variables using java.lang.reflect package - I do not understand the full concepts since I did not get time to look at the code and API. I will update you when I get more info. But I was told that the idea of not giving access to private methods and properties is to force encapsulation and not as a security measure. Hence it is OK if another object gets the private properties using reflection for storing the objects state. It is unacceptable if you try do that in your application program.


------------------
Hari Gangadharan
Out of the turbulance a soft voice spoke to me ...
Son smile and be happy things could be worse...
I smiled and I was happy and things became worse!


<B>Hari Gangadharan</B><BR>Unix is user friendly..<BR>but it chooses to whom it is friendly with!
S Dave
Ranch Hand

Joined: Jan 28, 2001
Posts: 103
to add to what's been said:
there's a class called Class ie java.lang.Class, which has a method called forName() which returns an object of the specified class. this object can use methods of java.lang.reflect package to get instances of classes such as Field, Method, Constructor etc.
though all of this has little to do with the way variables can be accessed by objects of other classes.
Rahul Rathore
Ranch Hand

Joined: Sep 30, 2000
Posts: 324
Hari, the file format of serialization is much more complicated and involves a proprietary protocol. Details are given from page 669 of Core Java -I
Storage is binary not character. Not all objects are serialized as unicodes. Only character type (String/character) is unicode and even these are stored as UTF (which -roughly speaking - is an efficient form of unicode). Other data types are stored as they are in binary. For eg. a field of type integer and with value 5 will be stored in 4 bytes and will have the binary value 5. A long with value 5 will be stored in 8 bytes and will have binary value 5. Since these are binary values, we cannot read these files by manually opening them. We may be able to read only the Strings provided the system supports UTF encoding. But the bit patterns containing the value 5 may not be recognizable by the system as any character or may be recognized as some strange character (not as 5- as you yourself note). Thus whatever encoding your platform supports you will always see strange characters in the serialized file.
Originally posted by Hari Gangadharan:
The objects serialized are written to file in the form of unicodes. You may view it in many of the platforms since they support Unicode. If you open it in an editor whch will not support Unicode then you will not be able to see it. The funny characters you see may be the integer values -- for example integer 5 will be written as 5 whereas unicode value of character '5' is 53.


[This message has been edited by Rahul Rathore (edited March 13, 2001).]
 
Don't get me started about those stupid light bulbs.
 
subject: Serializable