• 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

use of static method in web development

 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I wanted to know if it is a good practice to have a java class with a static method which is called by servlets in different sessions to add objects of a particular type in that particular session.

My confusion is whether different sessions accessing a static method would behave weird.


Please let me know.

Thanks,
SM.
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Assuming it's thread-safe I don't see how it could act "weird", but I'm not sure what "weird" would be.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Newton wrote:Assuming it's thread-safe I don't see how it could act "weird", but I'm not sure what "weird" would be.



And it is that 'thread-safe' point which should be of most concern. Non-thread-safe methods (static or not) are often the cause of "weird" behavior.
 
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, there are no special issues with static versus instance methods. Presumably, your servlet is passing the HttpServletRequest or HttpSession to the static method, and the method updates the passed object's attributes...? Servlets are just Java classes, so work like any other piece of Java code. The only unusual aspect of servlets is that their service methods may be called by multiple threads, and even though people fret about that a lot, it rarely leads to problems.
 
Sam Venkata
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By "weird" what I meant was any possibility of session information overlap. I was thinking of this scenario to be specific.



I think it makes sense when it is thread safe.

Thanks, for your help.

SM.
 
Max Rahder
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(Not to belabor the point, but if your variable "map" is an instance field in "Actionextends", then you might have a problem. Plus, I'm not sure how sessions play a role in your code.)
 
Oh. Hi guys! Look at this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic