Reputation: 444
I'm newly started reading about Java Beans and I had a question which was exactly same as this Topic's question. So I repeat The question:
in definition it is said "java bean encapsulates many objects into one object(Bean)."
1.What does "many objects" here mean?
and
2.How they are encapsulated into one object by java beans?
Edit:
From Java Beans Wikipedia:
in computing based on the Java Platform, JavaBeans are classes that encapsulate many objects into a single object (the bean).
Edit2: all of classes have ability of having multiple attributes and fields. If encapsulating of many objects means having multiple attributes and fields, I don't understand why they mentioned to this ability as a advantage of java bean class.
Upvotes: 1
Views: 1284
Reputation: 2687
JAVA Beans
The concept of JavaBeans was originally devised for Swing to facilitate the development of standalone GUI components, but the pattern has been repurposed for the land of Spring beans and back-end persistence with Hibernate
On the other hand, POJOs are simple java classes.
Another View:
Any POJO on having interaction with some third party become JAVA BEAN :)
As they say, "With Great Powers Comes Great Responsibilities" [excerpt from spiderman]
So our normal Pojos become JAVA BEANS :)
An article worth going through: https://mossgreen.github.io/Java-Bean-VS-Spring-Bean/
Upvotes: 2
Reputation: 4667
First to make it clear, every Class
in Java extends the type Object
. Something like String
is also an Object
.
The "many objects" is referring to how we can use different objects as fields within the bean. This creates a has-a relationship with the bean to your Objects
.
For example, say we have this Bean
:
public class YourBean implements java.io.Serializable {
private String s;
private ArrayList<String> list;
//Omitted rest of bean boilerplate
}
This example will contain two different Object
s inside of it, the String s
and the ArrayList<String>
named list
. You can add as many different Objects
and primitives to your bean as you want.
To create the bean with a no-args constructor, you would then use:
YourBean bean = new YourBean();
And you can set and get the values of the Objects
encapsulated within with:
ArrayList<String> yourList = new ArrayList<>();
bean.setList(yourList);
System.out.println(bean.getList());
You will be able to refer to all the Objects
inside the bean this way by referencing the bean Object
I named bean
.
Additionally, you can create multiple of the same type of bean
as well, so every time you make a new YourBean()
, you will also be able to use all the Objects
contained within.
This functionality is not unique to a Bean
, you can do this in any Class
, rather a Bean
is a term used to describe a specific way you write some classes.
I recommend looking into Java Composition to learn when you should use a has-a relationship, rather than inheritance which is an is-a relationship.
Upvotes: 3
Reputation: 81
We usually talk about Spring beans, but I think that's not what you're talking about. It seems to me that those JavaBeans are nothing but classes with multiple attributes and only getters/setters but whose constructor has zero arguments (and therefore it is mutable). As simple as that. The fact of encapsulating many objects is due to it having multiple attributes.
However, I have never referred to them as JavaBeans and I think the most similar concept I have ever worked with are the POJOs. I'm not sure if there is any difference, but the purpose looks the same.
If you ever talk about a bean in Java, I think anyone will think of a Spring bean. I suggest you not to use it in another context.
This is just my guessing. If I've said anything wrong please tell me.
Upvotes: 2