Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

static initialization in EJB  RSS feed

 
Ranadhir Nag
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where and how do i initialize a 'static final ' variable in my bean class?
Is a static initializer allowed ?
What else is a better alternative and why?
 
Ralph Jaus
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ranadhir,

the only restriction according static fields is:
EJB 3.0 spec (chapter 21.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 be declared as final.
So it should be possible to put your initialization into a static initialization block as well as into the no-arg constructor.
 
devendra bargujar
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Ranadhir -

I would suggest you creating seperate CONSTANT java file. Which should be Interface with all your attributes. Now create 'is a' relationship of Interface with your Bean class.

Reason
1) Putting into Interface automatically complies your attributes to be Static and Final thus small help for toward your design.

Regards... Devendra
 
Ranadhir Nag
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Initializing static final fields in no-arg ctor?
I thought such fields need to be initialized before the first instance of a class comes into existence ,which essentially means a staci block or initialization at declaration.
Initializing at ctor is not an option - correct me if i am wrong on this.

Secondly,for Session EJB I do not ever remember having provided a ctor explicitly.However,theoretically,for a session EJB,what is the difference in initializing a member final variable(non-static) in ctor and ejbCreate methods.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
devendra bargujar wrote:Hello Ranadhir -

I would suggest you creating seperate CONSTANT java file. Which should be Interface with all your attributes. Now create 'is a' relationship of Interface with your Bean class.

Reason
1) Putting into Interface automatically complies your attributes to be Static and Final thus small help for toward your design.

Regards... Devendra


If you are using this to define constants why would you use an Interface rather than an enumeration?
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ranadhir Nag wrote:Initializing static final fields in no-arg ctor?
I thought such fields need to be initialized before the first instance of a class comes into existence ,which essentially means a staci block or initialization at declaration.
Initializing at ctor is not an option - correct me if i am wrong on this.

Secondly,for Session EJB I do not ever remember having provided a ctor explicitly.However,theoretically,for a session EJB,what is the difference in initializing a member final variable(non-static) in ctor and ejbCreate methods.


What is a ctor?

 
Ralph Jaus
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ranadhir Nag wrote:
Initializing static final fields in no-arg ctor?
I thought such fields need to be initialized before the first instance of a class comes into existence ,which essentially means a staci block or initialization at declaration.
Initializing at ctor is not an option - correct me if i am wrong on this.

Yes, of course, you're right ! (how can one write such a nonsense like I did )

Ranadhir Nag wrote:
However,theoretically,for a session EJB,what is the difference in initializing a member final variable(non-static) in ctor and ejbCreate methods.

According to Java rules (vindication, yeah ) a final non-static variable must have been initialized after an instance of a class has been created (i.e. after a constructor has been executed). So, if you initialize a final non-static variable in ejbCreate method the (ejb) class won't compile.

In general the difference between the no-arg constructor and post-construct / ejbCreate methods is that in the latter you can use some methods from ejb context or do JNDI lookups that aren't allowed in the constructor.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!