Professional Documents
Culture Documents
Java - What Is A Javabean Exactly?: Edit: If You Can Be So Kind and Add Information Regarding The
Java - What Is A Javabean Exactly?: Edit: If You Can Be So Kind and Add Information Regarding The
Java - What Is A Javabean Exactly?: Edit: If You Can Be So Kind and Add Information Regarding The
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?
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.
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.
1 of 8 1/23/19, 5:30 PM
java - What is a JavaBean exactly? https://stackoverflow.com/questions/3295496/what...
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":
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.
2 of 8 1/23/19, 5:30 PM
java - What is a JavaBean exactly? https://stackoverflow.com/questions/3295496/what...
Properties of JavaBeans
3. All JavaBean properties must have public setter and getter methods
Example of JavaBeans
@Entity
public class Employee implements Serializable{
@Id
private int id;
private String name;
private int salary;
public Employee() {}
3 of 8 1/23/19, 5:30 PM
java - What is a JavaBean exactly? https://stackoverflow.com/questions/3295496/what...
Java Beans are using for less code and more work approach... Java Beans are used
throughout Java EE as a universal contract for runtime discovery and access. For
example, JavaServer Pages (JSP) uses Java Beans as data transfer objects between
pages or between servlets and JSPs. Java EE's JavaBeans Activation Framework uses
Java Beans for integrating support for MIME data types into Java EE. The Java EE
Management API uses JavaBeans as the foundation for the instrumentation of
resources to be managed in a Java EE environment.
About Serialization:
After a serialized object has been written into a file, it can be read from the file and
deserialized that is, the type information and bytes that represent the object and its
data can be used to recreate the object in memory.
You will find Serialization useful when deploying your project across multiple servers
since beans will be persisted and transferred across them.
4 of 8 1/23/19, 5:30 PM
java - What is a JavaBean exactly? https://stackoverflow.com/questions/3295496/what...
1. import java.io.Serializable
2. private fields
Fields should be private for prevent outer classes to easily modify those fields. Instead
of directly accesing to those fields, usuagly getter/setter methods are used.
3. Constructor
4. getter/setter
/** 3. Constructor */
public User() {
}
public User(int id, String name) {
this.id = id;
this.name = name;
}
/** 4. getter/setter */
// getter
public int getId() {
return id;
}
public String getName() {
return name;
}
// setter
public void setId(int id) {
this.id = is;
}
public void setName(String name) {
this.name = name;
}
}
5 of 8 1/23/19, 5:30 PM
java - What is a JavaBean exactly? https://stackoverflow.com/questions/3295496/what...
1. The class must have a public default constructor (with no arguments). This
allows easy instantiation within editing and activation frameworks.
2. The class properties must be accessible using get, set, is (can be used for
boolean properties instead of get), and other methods (so-called accessor
methods and mutator methods) according to a standard naming convention.
This allows easy automated inspection and updating of bean state within
frameworks, many of which include custom editors for various types of
properties. Setters can have one or more than one argument.
Java Beans is a standard, and its basic syntax requirements have been clearly
explained by the other answers.
However, IMO, it is more than a simple syntax standard. The real meaning or
intended usage of Java Beans is, together with various tool supports around the
standard, to facilitate code reuse and component-based software engineering, i.e.
enable developers to build applications by assembling existing components (classes)
and without having to write any code (or only have to write a little glue code).
Unfortunately this technology is way under-estimated and under-utilized by the
industry, which can be told from the answers in this thread.
If you read Oracle's tutorial on Java Beans, you can get a better understanding in that.
6 of 8 1/23/19, 5:30 PM
java - What is a JavaBean exactly? https://stackoverflow.com/questions/3295496/what...
A Java Bean is a java class [conceptual] that should follow following conventions:
It is a reusable software component. It can encapsulate many object into one object so
that same object can be accessed from multiples places and is a step towards easy
maintenance of code.
The objects that form the backbone of your application and that are managed by the
Spring IoC container are called beans. A bean is an object that is instantiated,
assembled, and otherwise managed by a Spring IoC container. Otherwise, a bean is
simply one of many objects in your application. according to spring io.
7 of 8 1/23/19, 5:30 PM
java - What is a JavaBean exactly? https://stackoverflow.com/questions/3295496/what...
Good to note the serialVersionUID field is important for maintaining object state.
Below code qualifies as a bean:
/** 4. getter/setter */
// getter(s)
public int getId() {
return id;
}
public String getNameOfDog() {
return nameOfDog;
}
// setter(s)
public void setId(int id) {
this.id = id;
}
public void setNameOfDog(String nameOfDog) {
this.nameOfDog = nameOfDog;
}}
8 of 8 1/23/19, 5:30 PM