File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes EJB and other Java EE Technologies and the fly likes Static fields in Entity Bean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Static fields in Entity Bean" Watch "Static fields in Entity Bean" New topic

Static fields in Entity Bean

Fisher Daniel
Ranch Hand

Joined: Sep 14, 2001
Posts: 582
Dear all
In EJB 2.0 Specification Chapter 24 p. 494, there is statement
"An enterprise Bean must not use read/write static fields. Using read-only static fields is allowed.
Therefore, it is recommended that all static fields in the enterprise bean class be declared as final."
What's the meaning of that statement especially about read/write static fields?
Example I have a static ArrayList variable, when I add some objects into that variable, is it true that I write static field?
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8927

Since EJB are distribued objects, only static final read only varialbles are allowed.
If you declare a Arraylist as a final static and modify the contents of the list you are viloating the spec because the arraylist in the same EJB in another JVM will *not* see the changes. So whenever using static avraibles use static final and for *read-only* purpose.
[ September 07, 2003: Message edited by: Pradeep Bhat ]

Marin van Zandvoort

Joined: Mar 04, 2004
Posts: 5

I don't quite understand the spec's recommendation.
If declaring an object 'static final' doesn't prevent de object from being modified, I wouldn't be putting that phrase in the spec.

Am I perhaps missing something here?

Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 33102

final prevents the reference from being modified. For some objects like Strings, this is enough. For other objects, you can call a set method and modify the object's state. Which doesn't meet the spirit of the final rule.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
I agree. Here's the link:
subject: Static fields in Entity Bean
It's not a secret anymore!