This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes A can full of Beans (Questions) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "A can full of Beans (Questions)" Watch "A can full of Beans (Questions)" New topic
Author

A can full of Beans (Questions)

Gavin Tranter
Ranch Hand

Joined: Jan 01, 2007
Posts: 333
Ok, well two

Is a bean allowed and equals and hashCode method?
Do all fields in a bean have to have both getters and setters?

I am thinking its okto have equals and hashCode, and that you may wish to represent read only properties, and thus, not having a setter is acceptable.

Cant think of a case for having a setter but not a getter.

G
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Cant think of a case for having a setter but not a getter.



This example may lend you a sapling of giant orchard.

Regards,
cmbhatt
[ April 11, 2007: Message edited by: Chandra Bhatt ]

cmbhatt
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

"Is a bean allowed and equals and hashCode method?"

Yes, a bean is just a class, just like any other class, so it can have an equals and hashCode method - in fact it might even be a good idea to provide these methods.

"Do all fields in a bean have to have both getters and setters?"

No, only the fields that you want to expose to the world outside the bean. The bean might have fields that are for internal use only. And indeed you can have read-only properties for which you only provide a getter and no setter.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Gavin Tranter
Ranch Hand

Joined: Jan 01, 2007
Posts: 333
Thanks guys,
For some reason I find beans Intimidating.
Stuart Ash
Ranch Hand

Joined: Oct 07, 2005
Posts: 637
Avoid the Heinz one and you ll be fine


ASCII silly question, Get a silly ANSI.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A can full of Beans (Questions)
 
Similar Threads
overriding hashcode and equals method internal work flow
I defined an equals method, but Hashtable ignores it. Why?
LearnKey Bonus Exam question
Object responsibility with persistence
hashCode() implementation