Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Fundamental Doubt about static methods

 
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello there Ranchers !

My mind is broken, I feel I can't access the entirety of my knowledge on EJB 3(or otherwise I need more practice). I wish I could be helped with the following question(s)..

Is it valid to have static methods in an EJB 3 class ?

I wanted to know especially if there's a problem with having static methods that are marked with life-cycle annotations like @PostConstruct.

Would I happen to obtain the same results given a Session Bean or a Message Driven Bean ?

That'd be it for now,

Thanks in advance,

Jose

 
Ranch Hand
Posts: 5397
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dont know about EJB3 but in EJB2.x and in general also, it does not seem to be good practice to have Static methods in Remote class.
You will not be able to access it remotely.
 
Jose Campana
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello !

I think I may need to know more. you see, I'd like to know what's the meaning of this 'bad practice' when I use annotations especially.

Is it valid at all?

All clarifications are appreciated.

Jose
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use of annotations doesn't change the fundamentals. It doesn't make any sense to use static methods in an EJB. The container is going to call all methods as instance methods anyway. I would go further to argue that static methods in any code (outside of statup code) in an EJB application is bad practice. Static methods will limit the ability to scale the application and increase the risk of something turning up that is not threadsafe.
 
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I wanted to know especially if there's a problem with having static methods that are marked with life-cycle annotations like @PostConstruct.



@PostConstruct is a lifecycle callback method. As per the EJB3 spec (section 12.4):

EJB3 Spec, Section 12.4 wrote:Lifecycle callback interceptor methods can have public, private, protected, or package level access. A lifecycle callback interceptor method must not be declared as final or static.

 
Jose Campana
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh !

This is so cool. I had a vague idea about why I shouldn't mark EJB business methods or Lyfecycle methods as static. Now I have a clearer picture, for that I must say Thanks.
I feel I need to ask about something that was stated by Jaikiran Pai....

"A lifecycle callback interceptor method must not be declared as final or static"



However the compiler won't stop me if I mark them as static, why is this ?

Is this a case of: "Just because you can do it, doesn't mean it's right" ?

That's a final thought I wanted to share... I'll be expecting your comments,

As always thank you very much !

Jose
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic