This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes final static variables in beans? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "final static variables in beans?" Watch "final static variables in beans?" New topic
Author

final static variables in beans?

Vishwa Kumba
Ranch Hand

Joined: Aug 27, 2003
Posts: 1064
I know you cannot have final or static methods in a bean.
But I can use final static variables in my beans. Can't I?
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8904

Yes you can use static final variable for read only purpose.


Groovy
Vishwa Kumba
Ranch Hand

Joined: Aug 27, 2003
Posts: 1064
I meant final variables and static variables. Can they be used in a bean?
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8904

http://www.coderanch.com/t/312646/EJB-JEE/java/Why-we-static-EJB
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8904

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.
Vishwa Kumba
Ranch Hand

Joined: Aug 27, 2003
Posts: 1064
I assume we can use static variables and final static variables, even if the bean is in a passivated state or does not exist state, as static variables do not depend on the bean instance.Right?
Vishwa Kumba
Ranch Hand

Joined: Aug 27, 2003
Posts: 1064
Thanks Pradeep!!
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8904

static variables are not passivated.
Vishwa Kumba
Ranch Hand

Joined: Aug 27, 2003
Posts: 1064
Looks like our posts went a bit out of synch.
No problem!....I got the idea.
You can use static final variables but not static variables in a bean.
Cheers,
Vish
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8904

Read the page 71 of the spec. That will explain you more.
Vishwa Kumba
Ranch Hand

Joined: Aug 27, 2003
Posts: 1064
thanks pradeep,
I read the section, but it does not tell me if final variables will be passivated. Also I found the section: 24.1.2(p:494) - programming restrictions which mentions about the static, final variables.
"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.
This rule is required to ensure consistent runtime semantics because while some EJB Containers may use a single JVM to execute all enterprise bean�s instances, others may distribute the instances across
multiple JVMs."
Cheers,
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8904

Why can't I use nonfinal static fields in my enterprise bean?
Nonfinal static class fields are disallowed in EJBs because such fields make an enterprise bean difficult or impossible to distribute. Static class fields are shared among all instances of a particular class, but only within a single Java Virtual Machine (JVMTM). Updating a static class field implies an intent to share the field's value among all instances of the class. But if a class is running in several JVMs simultaneously, only those instances running in the same JVM as the updating instance will have access to the new value. In other words, a nonfinal static class field will behave differently if running in a single JVM, than it will running in multiple JVMs. The EJB container reserves the option of distributing enterprise beans across multiple JVMs (running on the same server, or on any of a cluster of servers). Nonfinal static class fields are disallowed because enterprise bean instances will behave differently depending on whether or not they are distributed.

It is acceptable practice to use static class fields if those fields are marked as final. Since final fields cannot be updated, instances of the enterprise bean can be distributed by the container without concern for those fields' values becoming unsynchronized.
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8904

http://java.sun.com/blueprints/guidelines/designing_enterprise_applications/ejb_tier/qanda/restrictions.html
Vishwa Kumba
Ranch Hand

Joined: Aug 27, 2003
Posts: 1064
Thanks Pradeep!
That was really a good link.
Kathy Sierra
Cowgirl and Author
Ranch Hand

Joined: Oct 10, 2002
Posts: 1572
Wow! Thanks for such a great response Pradeep. I'll just pretend that I would have said it just as well, but I seriously doubt it...
cheers,
Kathy
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: final static variables in beans?
 
Similar Threads
Can I use Static Variable in Ejb class?
A member interface is implicitly static
static variables in beans
Static and Non-static Methods
By convention - are there for exemple first variables, second getters and setters and third methods?