Cristiano Coelho
Cristiano Coelho

Reputation: 1735

Java Serialization and JavaBeans

Quick question, when marking an object as serializable, does it need to be a JavaBean? I mean, can you serialize an object that's not a JavaBean? Does it have any risk? Is it a good practice to always make an object a JavaBean if you intend to serialize it?

Upvotes: 2

Views: 7610

Answers (4)

Fabian Trottmann
Fabian Trottmann

Reputation: 224

Serializable is a marker Interface. Each Object you mark with the serializable interface can be sent trouh the wire or can be safed in a file. For example if you mark the class Foo with the serializable interface, you are able to safe the object state in a file and restore it later:

public class Foo implements java.io.Serializable{  
  public String name;
}


public main(){
 Foo foo = new Foo();
 foo.name="test";
  try
  {
     FileOutputStream fileOut = new FileOutputStream("foo.file");
     ObjectOutputStream out = new ObjectOutputStream(fileOut);
     out.writeObject(foo);
  }
}

That means it doesnt need to be a JavaBean. It could be a plain old java object, like the Foo Object example.

Upvotes: 2

Firenze
Firenze

Reputation: 308

You can serialize any object that implements the Serializable interface, whether it's a JavaBean or not.

That said, the decision to make an object Serializable shouldn't be made lightly, because it locks in certain implementation details of the class thus reducing future flexibility.

See here for information on implementing Serializable.

Upvotes: 0

hvgotcodes
hvgotcodes

Reputation: 120178

You are looking at it the wrong way. A Java Bean is any class that is

1) implements Serializable
2) Has a no-arg constructor
3) Has private members and setters/getters

So your question

marking an object as serializable, does it need to be a JavaBean?

has it backwards. Any class can be Serializable, by implementing the interface. Not all serializable classes define a Java Bean.

I mean, can you serialize an object that's not a JavaBean?

Yes.

Is it a good practice to always make an object a JavaBean if you intend to serialize it?

It is good practice to design your classes with data encapsulation in mind. This means limiting access to fields directly, and using setters and getters where appropriate.

Of course, having a public no-arg constructor is not always necessary from an API point of view.

You really only need to follow the Java bean standard if you are going to use a library that depends on your classes being Java Beans.

Upvotes: 13

kosa
kosa

Reputation: 66637

If you want to serialize an object of class, then that class need to implement serializable interface irrespective of it's bean (or) class with simple properties.

To serialize an object means to convert its state to a byte stream so that the byte stream can be reverted back into a copy of the object. A Java object is serializable if its class or any of its superclasses implements either the java.io.Serializable interface or its subinterface, java.io.Externalizable. Deserialization is the process of converting the serialized form of an object back into a copy of the object

This tutorial may help you

Upvotes: 0

Related Questions