• Post Reply Bookmark Topic Watch Topic
  • New Topic

JavaBean  RSS feed

 
pankaj vijay
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I have problem in javabeans. why we use java bean? whats the exact use of java bean. I mean we declare field as private & setter,getter as public but why?
because if user access field directly then also there is no problem . For Eg.


usually user access setter not field because we have declare field as private. But what's wrong if i declare field as public so that user can access field.In both condition user is assigning some value in name field that's it.
Then why we use this pattern?
 
Leandro Coutinho
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We can realize that a basic concept like this is not covered in certifications.
If the field is public you can get invalid input. Make the field private forces to use the setter method, so you can handle the input.
An example can be an email address.
If someone pass: myemail@, it will be stored in the variable, even though it's an invalid email.
Other example is an age field and someone pass a negative value.
You will also benefit from many technologies and frameworks that rely on this convention.
Other thing is to don't break others code. You may don't need to use any restrictions now, but maybe you'll need to add some validation or do some operations and then you'll break compatibility.
 
pankaj vijay
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Leandro..Dont you think that beans are just POJO ... you should not use business logics in beans.
as per my knowledge this is not the answer....

somewhere i heard that beans are based on Value Object design pattern. but whats the use of this. & how it is useful? i am not aware of this. Any idea???
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As far as I remember: a bean has a no-arguments constructor and public "set" and "get" methods for all its fields. If you put the bean into a file, its class and all its fields are copied into an XML file (kept as text). When you re-create the object, all its details are copied from the XML and put into the "set" methods.
 
Leandro Coutinho
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.
pankaj vijay wrote:Leandro..Dont you think that beans are just POJO ... you should not use business logics in beans.
as per my knowledge this is not the answer....

somewhere i heard that beans are based on Value Object design pattern. but whats the use of this. & how it is useful? i am not aware of this. Any idea???

I didn't find any good definition for POJO.
I think bean is not POJO. POJO doesn't need to implement any interface and bean should implement Serializable.
The Value Object design patter (also known as Data Transfer Object) is described here.
There's nothing in JavaBeans that prohibits you to use any logic in your code. If it's a good practice or not, I would like to find some source about this.
So why do validation in the model?
This quote is from the book Agile Web Development with Rails, Third Edition
The model layer is the gatekeeper between the world of code and the database. Nothing to do with our application comes out of the database or gets stored into the database that doesn't first go through the model. This makes models an ideal place to put validations ...

I like to use validations in the java beans, because otherwise I think you have great chances to have your validations repeated in your code. Of course you can avoid this if you have a good design.

Where do you put your validation?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!