• Post Reply Bookmark Topic Watch Topic
  • New Topic

static variables in beans

 
paul wheaton
Trailboss
Posts: 22062
Firefox Browser IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it true that static variables are not allowed in beans? Why?
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think it is true.
 
paul wheaton
Trailboss
Posts: 22062
Firefox Browser IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why?
(btw-- appropriate names are now required, see http://www.javaranch.com/ubb/Forum10/HTML/000180.html for details on creating a new account)
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Static variables are allowed in an EJB. See: http://developer.java.sun.com/developer/onlineTraining/Beans/EJBTutorial/step4.html
Scroll to the first example and you will see that the first variable listed in that session bean is static.
 
paul wheaton
Trailboss
Posts: 22062
Firefox Browser IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out this info from George:
from the ejb 1.1 spec, section 18.1.2
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 bedeclared 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 beans
instances, others may distribute the instances across multiple JVMs.
George: Where does one find this lovely document?
 
George Brown
Ranch Hand
Posts: 919
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is correct. All static variables must be final although in 99% of EJB servers, using a non-final static variable wouldn't cause a problem.
 
phil ranger
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to use static variables in EJB to cache stuff using lazy loading.
It wouldn't matter if the state is different across different copies of a
class.
Is there any reason I couldn't / shouldn't use this approach?
Doing this kind of thing should surely improve performance while keeping
code clean.
Are there better alternatives to this pattern?
ps - I'm using JBoss but want to keep my code portable and able to run OK
in a cluster.
example code:
public class EJBUtils {

private static InitialContext mInitialContext = null;
public static Context getContext(){
if( mInitialContext == null){
mInitialContext = new InitialContext();
}
return mInitialContext
}
private static mPersonLocalHome = null;
public static PersonLocalHome getPersonHome(){
if ( mPersonLocalHome == null){
mPersonLocalHome = getContext().lookup(
PersonLocalHome.JNDI_NAME);
}
return mPersonLocalHome;
}
}
 
Pradeep bhatt
Ranch Hand
Posts: 8933
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why dont you use Service Locator pattern?
java.sun.com/blueprints/corej2eepatterns/ Patterns/ServiceLocator.html
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!