Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

bean method question

 
Mark Stein
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know a bean has to have the following...
1) Implement java.io.Serializable or java.io.Externalizable
2) have a no-arguments constructor
3) use the get/set naming patterns to access the beans private properties.
My question is on the 3rd one... is it legal to have a bean with just a get method? What do you do if you want to be able to retrieve a value from a bean, but you don't want it settable.
Mark Stein
 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mark Stein:

3) use the get/set naming patterns to access the beans private properties.
My question is on the 3rd one... is it legal to have a bean with just a get method? What do you do if you want to be able to retrieve a value from a bean, but you don't want it settable.

By doing that you get a Read-Only property for the Bean. By doing the opposite (Only having a setter) you get a Write-Only property for the bean. --It's totally legal
[ July 16, 2002: Message edited by: Jessica Sant ]
 
Jignesh Malavia
Author
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mark Stein:
My question is on the 3rd one... is it legal to have a bean with just a get method? What do you do if you want to be able to retrieve a value from a bean, but you don't want it settable.
Mark Stein[/QB]

Yes, it is perfectly alright to have-
a) only a public getter method, if you want the bean user to be able to just access the property's value but not mutate it.
b) only a public setter method, if you want the bean user to be able to just mutate the property's value but not access it.
Further, if a method is present (getter or setter) but is non-public, then for the bean user, it is as good as if it was not present.
-j
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic