Results 1 to 4 of 4
Thread: Serializable problem
- 11-30-2010, 09:37 AM #1
Member
- Join Date
- Jun 2010
- Posts
- 9
- Rep Power
- 0
Serializable problem
i can't figure out what's wrong, but what i'm expecting from the last line System.out.println is to print the name Catty, but the string is empty. why?
Java Code:import java.io.FileInputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.FileOutputStream; import java.io.Serializable; public class SerializeCat implements Serializable { public static void main(String[] args) { Cat cat1 = new Cat("Catty"); Cat cat2 = new Cat(); try { ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("cat.ser")); out.writeObject(cat1); out.close(); } catch (Exception e) {} try { ObjectInputStream in = new ObjectInputStream(new FileInputStream("cat.ser")); cat2 = (Cat) in.readObject(); in.close(); } catch (Exception e) {} System.out.println("The name of cat2 is " + cat2.getName()); } }
the class Cat.java
Java Code:public class Cat { private String name; public Cat(String name) { this.name = name; } public Cat() { this(""); } public String getName() { return name; } public void setName(String name) { this.name = name; } }
- 11-30-2010, 09:38 AM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Make the class Cat serialisable, not the class that is testing it's serialisation capability.
- 11-30-2010, 09:45 AM #3
Member
- Join Date
- Jun 2010
- Posts
- 9
- Rep Power
- 0
- 11-30-2010, 10:54 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Similar Threads
-
marking a field Serializable
By user1234 in forum Advanced JavaReplies: 9Last Post: 05-18-2010, 11:27 AM -
Java reflection: find serializable classes in package
By andrew222 in forum Advanced JavaReplies: 2Last Post: 02-27-2010, 07:36 AM -
Serializable problem in writeObject method
By nouvaki in forum New To JavaReplies: 3Last Post: 01-16-2010, 01:56 PM -
Casting to an List<Serializable>
By Whatty in forum Advanced JavaReplies: 5Last Post: 10-15-2009, 07:47 PM -
Implementing Serializable interface
By javaplus in forum Advanced JavaReplies: 4Last Post: 12-18-2007, 12:29 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks