• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Stateless Object

 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So I've heard of stateless objects in VB (shudder with fear and disgust) but I have not heard of them in Java...until now. In discussion with someone they brought it up so I put it forth:
What is a stateless object in Java (if there is such a thing) and what is it used for (or when does it occur)?
Thanks for your help, all.
Have a good one.
Paul
 
author
Posts: 3252
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Paul Caudle:
What is a stateless object in Java (if there is such a thing) and what is it used for (or when does it occur)?


An object without state, which roughly translates to: without member variables.
You would prefer to use stateless objects in multi-threaded contexts, because they are inherently threadsafe. Common examples are JSPs and servlets. Rather than maintaining any state themselves, they pull their state from their context -- the request, the session, the servlet context.
JSPs and servlets which need to maintain state usually end up implementing the SingleThreadModel. This causes the server to never run more than a single request through them. This is because JSP programmers are generally wimps
You will also find stateless objects in certain design patterns, most notably the Flyweight pattern from the GoF book. Flyweights are used where you need lots of objects which are essentially identical; by removing all state from them, you never need to create more than one copy for each type of object. (They gave the rather convoluted example of representing each single character in a document using objects).
- Peter
 
Paul Caudle
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hiya Peter,
Thanks for the explanation. It makes sense after thinking through it. So not only does the object not have values for any member variables, but it does not contain the possibility for having values because member variables don't exist.
Seems like an obvious solution to multithreading woes, but it may end up being a little taxing with all of the local variables that may be used.
Thanks again for the Response, Peter.
Paul
PS...where is Mary when you need her?
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
LOL
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The stateless class may have the local variable as long as they are static final i.e. they must not allow any change
reply
    Bookmark Topic Watch Topic
  • New Topic