Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between java bean and java class?

Tags:

java

javabeans

I am new to the JSP and server side programming. Till now I am working with Servlets and java classes. I am segregating my application (as per MVC model) with the help of java classes. I would like to know difference between java beans and java classes. And in which scenario I can use a java bean instead of a java class. Any helpful explanation or helpful links?

like image 574
Raj Avatar asked Dec 25 '11 10:12

Raj


People also ask

What is difference between Java class and bean?

The only difference between both the classes is Java make java beans objects serialized so that the state of a bean class could be preserved in case required.So due to this a Java Bean class must either implements Serializable or Externalizable interface.

Is a JavaBean a class?

A JavaBean is a specially constructed Java class written in the Java and coded according to the JavaBeans API specifications. It provides a default, no-argument constructor. It should be serializable and that which can implement the Serializable interface.

What is the difference between Java class and spring bean?

Spring bean is managed by Spring IOC, Java Bean is not. Java Bean is always serializable, Spring Bean doesn't need to. Java Bean must have a default no-arg constructor, Spring Bean doesn't need to. A Java object can be a JavaBean, a POJO and a Spring bean all at the same time.


2 Answers

A Java bean is just a class which conforms to some conventions:

  • properties that can be accessed by getters (and setters if those properties are not read-only)
  • no-arg public constructor
  • serializable

The JSP EL and tags are designed around those conventions. Most of them don't need all these conventions to be respected. properties available by getters is the most important of these conventions. For example, the expression

${foo.bar.name}

displays the name of the bar of the foo bean. foo is a bean that must be in the page, request, session or application context. And this expression will call getBar() on this bean, and then getName() on the object returned by getBar().

like image 65
JB Nizet Avatar answered Oct 02 '22 20:10

JB Nizet


The JavaBeans specification defines the type JavaBeans components as "reusable software components". A component is a simple Java Bean Class Java respects certain conventions about method naming, construction and behavior. Adherence to these conventions makes it possible to use, reuse, replace and connect Java Beans for development tools. The beans must be "Serializable"In order to save and restore instances of this class.

like image 26
amod Avatar answered Oct 02 '22 21:10

amod