Basic Question about Java Bean

I just started working with JavaBean. I have been reading the concepts involved but I still have problems understanding the difference between a normal object and a bean . Can anyone tell the exact difference ?
[225 byte] By [sun_zorro] at [2007-9-26 3:10:55]
# 1
I'm also new to javaBeans but I read that the difference seems to be some constraints about the properties (simple values or arrays) These properties have to be serializable.
CTT at 2007-6-29 11:17:55 > top of Java-index,Desktop,Developing for the Desktop...
# 2

Also, is it true that creating a JavaBean is done primarily with the intention of using it graphically (e.g. in an IDE)? My understanding is that EJBs are used throughout the J2EE architecture, but that a class with attributes, accessor and mutator methods technically constitute a JavaBean. However, it appears that some level of serialization is required to be present as well.

Please correct me, if I am mistaken. Recruiters are wanting to know why I don't have "JavaBeans" on my resume when I try to explain it to them. Uugghh.

TIA.

Jeff

marendoj at 2007-6-29 11:17:55 > top of Java-index,Desktop,Developing for the Desktop...
# 3

I don't know about beans being "primarily" for graphical use. That seems to be a natural way to use them, and a lot of the examples tend to stress that. From my own experience, I don't develop graphical components, and while I've written several bean classes, none of them has been graphical.

The real thing that separates beans from non-bean classes has to do with naming conventions. That is, if you have a class with a "getX" method and a "setX" method, then anything that wants to look at that class as a bean, then, as a bean, the class is interpreted to have a read/write property "X".

Bound properties, indexed properties, etc. go beyond that, but really, that's the heart of it.

jverd at 2007-6-29 11:17:55 > top of Java-index,Desktop,Developing for the Desktop...
# 4

Definition

To define a JavaBean is a pretty difficult task. It is easy enough to draw lines between JavaBeans and EJBs, as the first are primarily used in client-side applications and the latter are exclusively used on the server.

"JavaBean" is just Sun's old term for a java class or a java object. I think that at some point in history, James Gosling or some another Java-architect got the idea that a great deal of all future java classes should be designed as beans. The general idea here is: a really superbly designed class is more than just a class. It can go out into the world and do a lot of good. It can call home and bring you money. It can fall in love and be eaten by others which turns them into beans as well.

No, lets get serious.

The idea of "beaning" everything stems from the naive mid-ninetees idea that components are more useful than programs and that expensive programmers will be replaced by wallpapers of beans in the future. What an idea!

There are two surviving packages of bean-stuff in the JSDK. Look at them if you like.

The most important one:

<i>java.beans</i>

contains some interfaces such as

BeanInfo

that forces you to add documentation support to your class.

The primary ideas introduced in these two packages are:

1. Properties. (Bound and constrained)

2. BeanInfo.

3. Hierarchies of executable beans.

4. The ability to make a movable graphical component that can be used to build applications visually in IDE's

The other package

<i>java.beans.beancontext</i>

"Provides classes and interfaces relating to bean context. A bean context is a container for beans and defines the execution environment for the beans it contains." (Taken from the class description in the API.)

Hope this was of some help.

Morten Hjerl-Hansen

Kepler222 at 2007-6-29 11:17:55 > top of Java-index,Desktop,Developing for the Desktop...
# 5
A Java Bean is a java class that implements the Serializable interface. A bean has private attributes and public get/set methods to access the attributes. That is all you need to create a java bean.
wgower at 2007-6-29 11:17:55 > top of Java-index,Desktop,Developing for the Desktop...
# 6
sorry forgot one thing....a no-args public constructor
wgower at 2007-6-29 11:17:55 > top of Java-index,Desktop,Developing for the Desktop...
# 7

Hi, I was just searching thru the forums, and found these messages, Since my confusion is similar to the ones posted here, I am continuing this topic.

I have to work on MVC based architecture, and I have created a JSP page that calls a servlet. The servlet creates the instance of the class (which I call my bean class), and forwards to another jsp page. My (bean) class has overloaded constructors, one empty and the other takes a String as input. My servlet calls the constructor with the String parameter.

So, does that mean that is not a bean but a simple class ? I am new to Java, and I need guidance to clear few of my basics.

Thanks for any help

anusham at 2007-6-29 11:17:55 > top of Java-index,Desktop,Developing for the Desktop...
# 8

Basiclly yes. Your bean is just a simple class.

Like one of the former post says, a java bean is just a spesialized class. In your case it is not so spesialized because you have a constructor with an argument.

One part of the definition of a bean is that the constructor has no argument.

There is no need to have a bean if a regular class does the work.

TheLaw at 2007-6-29 11:17:55 > top of Java-index,Desktop,Developing for the Desktop...
# 9

There is something needed to be mentioned, regarding beans.

A javabean is acollection of one or more java classes, often bundeled into a single *.jar file, that serves a self-contained , reusable component. It can be a discrete component used in building a user interface (gui) or a non -UI component such as a data module or comptation engine.

Like other components, JavaBeans are reuseable piece of code that can be updated with minimal impact on the testing of the program they become a part of.

It should be possible to serialize a customized and conected bean, so that its customized state can be saved and later restored.

An another important feature a bean should have is: itrospection.

The builder tool should be able to descide which methods, events and properties a bean supports, this is also a nice ting to have under runtime. This is called introspection.

The introspection is done in pure java and is based on the Java language support for reflection.

[SUN]:"Reflection enables Java code to discover information about the fields, methods and constructors of loaded classes, and to use reflected fields, methods and contructors to operate on their underlaying counterparts on objects"

In other words:

The introspection uses reflection to get beans methods and then it uses the design patterns that are stated for JavaBeans to deduce which property, events and public methods it supports.

The bean developer can provide a BeanInfo class that can be used instead of mentioned technique.

Regards TheLaw

TheLaw at 2007-6-29 11:17:55 > top of Java-index,Desktop,Developing for the Desktop...
# 10
Sorry, forgot to add a link: http://developer.java.sun.com/developer/onlineTraining/Beans/
TheLaw at 2007-6-29 11:17:55 > top of Java-index,Desktop,Developing for the Desktop...
# 11
Thanx for all ur replies - they are quite informative !
anusham at 2007-6-29 11:17:55 > top of Java-index,Desktop,Developing for the Desktop...
# 12

Here is the difference that I see from my experience with Java Beans:

You can write a Java class or a Java Bean to do similar things. For example, you can write an object that generates an event (example: generate a point x,y), you can write a listener or set of listeners (example: a graph to plot the x,y) to listen to this event and to respond to the event.

The difference is, when you write a simple java class, you hard code the list of listeners, i.e., in the code of the event generator you create an object of the listener within the generator and then notify those objects when an event is generated. If you create another listener object, you will have to modify the event generator code by adding this new listener in the code and recompile it.

With a bean, you create an object that generates an event (with an event object as the argument). In the code of the generator you don't have to specify which objects will be listening to the event. Then you can have several listener objects that listen to this event, obtain the event object and use the values in the event object to make any modifications in itself (for example a graph listener can plot the x,y obtained from the event object). Here is where you have the main difference, you can now dynamically connect the generator to one or several listeners (the events and listener methods are exposed through the descriptors). You can create a new listener and connect the generator to the new listener. You don't have to hard code the listeners into the generator. (I think that somehow in the background when you connect the event of the generator to the listener, a code is generated similar to the java classes that I mentioned above).

hsenapat at 2007-6-29 11:17:55 > top of Java-index,Desktop,Developing for the Desktop...