Win a copy of 97 Things Every Java Programmer Should Know this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

bean method question

 
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
 
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 ]
 
Author
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
 
Without subsidies, chem-ag food costs four times more than organic. Or this tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic