my dog learned polymorphism*
The moose likes EJB and other Java EE Technologies and the fly likes Static Methods in EJB 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 Methods in EJB" Watch "Static Methods in EJB" New topic
Author

Static Methods in EJB

Ram Dhan Yadav K
Ranch Hand

Joined: Aug 13, 2001
Posts: 321
Hi,
Can we use static methods in EJB's.
Are they allwed in Servlets?
thanks,
Ram


Ram Dhan Yadav (SCJP, SCWCD, SCJA-I, IBM EC(483))
"We are what we repeatedly do. Excellence, then, is not an act, but a habit."
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8904

Hi,
Business methods cannot be static since
java interfaces do not permit static methods.
There is no point in having static method in bean class.


Groovy
Peter Reinhardt
Ranch Hand

Joined: Aug 02, 2002
Posts: 57
Static methods are certainly possible in servlets (though they have to be implemented thread safe).
For EJBs I think it is possible if the method does nothing illegal (say a static helper method).
I don't think a static initializer makes a lot of sense because you are just allowed to have final static fields in an enterprise bean.

So the question is: What do you want to do in these static methods ?

Peter.


SCJP 1.2, SCJD, SCEA, IBM 484, Weblogic 7
Siva Jagadeesan
Ranch Hand

Joined: Oct 31, 2000
Posts: 160
We can use Static methods when we are using a Singleton pattern. There is a misunderstanding that we should NOT use singleton pattern in EJB. But we can use it if we know the consequences. There is a very good explanation for this is in "EJB Design Pattern" book, which is avialable free online


Rgds<p>--Siva Jagadeesan<br /><a href="http://java2simple.blog-city.com" target="_blank" rel="nofollow">http://java2simple.blog-city.com</a><br />Sun Certified Java2 Programmer<br />Sun Certified Web Component Developer<br />BEA Certified Weblogic Server 7 Enterprise Developer
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8904

What the book says is - Since the singleton pattern contains static method developers fear to use it and EJB spec says that static fields should be avoided.
Originally posted by Sivakumar Jagadeesan:
We can use Static methods when we are using a Singleton pattern. There is a misunderstanding that we should NOT use singleton pattern in EJB. But we can use it if we know the consequences. There is a very good explanation for this is in "EJB Design Pattern" book, which is avialable free online
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8904

If u read the EJB spec u will come to know that it forbids the declaration of static fields and static business methods but it does allow static
final fields.
Peter Reinhardt
Ranch Hand

Joined: Aug 02, 2002
Posts: 57
A static field is not the same as a static method which is not the same as a buisness method. and all of these have nothing to do with a singleton
In EJBs:
- static business method are not allowed.
- static fields are just allowed if they are final
- static methods (not business method) are in my opinion ok.
A singleton is not an EJB so the rule for the EJBs do not apply to it. If the EJB accesses the singleton it can do so in a business method for example (but it is not allowed to keep a reference to the singleton in a non final static field).
The singleton runs in the EJB container so the rules for thread mgmt. etc. still apply to it.

Peter.
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8904

Why would one want to use static non -business method ? It does not make sense at all.
The client can access the methods that are exposed in the interface and interfaces do not allow static method thereby disallowing static business methods.
If I have static (non - business) methods the only place where I can call it is some business method but the business method can only be called on an instance thereby the purpose of static method is defeated. U need to have an instance to call the static method. Why the need for static???
The benefit of static methods is there is no need to create an instance of a class but in EJB's instance are always created. So including static non business methods (even though leagal) does not make any sense. Any feed back is welcome.
[ August 07, 2002: Message edited by: Pradeep Bhat ]
Peter Reinhardt
Ranch Hand

Joined: Aug 02, 2002
Posts: 57
the static methods can be used as helper methods used by business method (static methods are not remotely visible). say you have something like "getConnection()" in a BMP entity bean.
I don't think that static methods are crucial in your EJB development, the question was initially just if you can do it, yes or no.
Peter
[ August 07, 2002: Message edited by: Peter Reinhardt ]
[ August 07, 2002: Message edited by: Peter Reinhardt ]
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8904

What is that anyone gains by making getConnection() as static.
Absolutely nothing.
I agree with u - the discussion here is abt whether we can have a static non business method. The answer is undoubtedly yes but nothing is gained from that.

Originally posted by Peter Reinhardt:
the static methods can be used as helper methods used by business method (static methods are not remotely visible). say you have something like "getConnection()" in a BMP entity bean.
I don't think that static methods are crucial in your EJB development, the question was initially just if you can do it, yes or no.
Peter
[ August 07, 2002: Message edited by: Peter Reinhardt ]
[ August 07, 2002: Message edited by: Peter Reinhardt ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Static Methods in EJB
 
Similar Threads
Static methods can be overridden by static methods only?
static and non-static synchronized methods
method overriding
Inheritance of all members
What is the logic behind calls like JOptionPane.method()