tags:

views:

227

answers:

6

I understood, I think, that a "Bean" is a Java class with properties and getters/setters. As much as I understand, it is the equivalent of a C struct. Is that true?

Also, is there a real syntactic difference between a bean and a regular class? Is there any special definition or an interface?

Basically, why is there a term for this, it puzzles me...

Edit: If you can be so kind and add information regarding the Serializable interface, and what it means, to your answer, I'd be very grateful.

+8  A: 

A java bean is just a standard

  1. All properties private (use getters/setters)
  2. a public No argument constructor
  3. Implements serializable.

thats it; AFAIK just a convention. Lots of libs depend on it though....

edit -- http://en.wikipedia.org/wiki/JavaBean

edit edit -- this is beginning to sound like homework. From the API: "Serializability of a class is enabled by the class implementing the java.io.Serializable interface. Classes that do not implement this interface will not have any of their state serialized or deserialized. All subtypes of a serializable class are themselves serializable. The serialization interface has no methods or fields and serves only to identify the semantics of being serializable."

In other words, serializable objects can be written to streams, and hence files, object dbs, anything really.

Also, there is no syntactic difference between a java bean and another class -- a class defines a java bean if it follows the standards.

There is a term for it because the standard allows libraries to programmatically do things with class instances you define in a predefined way. For example, if a library wants stream any object you pass into it, it knows it can because your object is serializable (assuming the lib requires your objects be proper java beans).

hvgotcodes
This isn't homework, I know how and what to tag and I would've tagged this 'homework' if it was. I was just interested. Thanks.
Amir Rachum
ok -- no offense intended -- thanx for the check mark.
hvgotcodes
+3  A: 

There's a term for it to make it sound special. The reality is nowhere near so mysterious.

Basically, a "Bean" is a serializable object (that is, it implements java.io.Serializable, and does so correctly) that has "properties" whose getters and setters are just methods with certain names (like, say, getFoo() is the getter for the Foo property), and that has a public default constructor (so it can be created at will and configured by setting its properties).

cHao
A: 

Regarding the second part of your question, Serialization is a persistence mechanism used to store objects as a sequence of signed bytes. Put less formally, it stores the state of an object so you can retrieve it later, by de-serialization. It's a binary mechanism, making it more space-efficient than a text-based persistence mechanism.

Mike
+2  A: 

JavaBeans are Java classes which adhere to an extremely simple coding convention. All you have to do is to

  1. Implement java.io.Serializable interface - To save the state of an object
  2. use a public empty argument constructor - To instantiate the object
  3. And provide public getter and setter methods - To get and set the values of private variables (properties ).
Kamal
A: 

You will find Serialization useful when deploying your project across multiple servers since beans will be persisted and transferred across them.

Truong Ha
A: 

Java beans are nothing but the reusable coponents. like dll in .net