What is a JavaBean exactly?
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?
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.
A JavaBean is just a standard
Serializable
. That's it. It's just a convention. Lots of libraries depend on it though....
With respect to Serializable
, from the API documentation:
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 databases, anything really.
Also, there is no syntactic difference between a JavaBean and another class -- a class defines a JavaBean 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 to stream any object you pass into it, it knows it can because your object is serializable (assuming the lib requires your objects be proper JavaBeans).
There's a term for it to make it sound special. The reality is nowhere near so mysterious.
Basically, a "Bean":
java.io.Serializable
, and does so correctly), that getFoo()
is the getter for the "Foo" property), and Update:
As for Serializable
: That is nothing but a "marker interface" (an interface that doesn't declare any functions) that tells Java that the implementing class consents to (and implies that it is capable of) "serialization" -- a process that converts an instance into a stream of bytes. Those bytes can be stored in files, sent over a network connection, etc, and have enough info to allow a JVM (at least, one that knows about the object's type) to reconstruct the object later -- possibly in a different instance of the application, or even on a whole other machine!
Of course, in order to do that, the class has to abide by certain limitations. Chief among them is that all instance fields must be either primitive types (int, bool, etc), instances of some class that is also serializable, or marked as transient
so that Java won't try to include them. (This of course means that transient
fields will not survive the trip over a stream. A class that has transient
fields should be prepared to reinitialize them if necessary.)
A class that can not abide by those limitations should not implement Serializable
(and, IIRC, the Java compiler won't even let it do so.)
JavaBeans are Java classes which adhere to an extremely simple coding convention. All you have to do is to
上一篇: Monad用简单的英语? (对于没有FP背景的OOP程序员)
下一篇: 什么是JavaBean?