• Post Reply Bookmark Topic Watch Topic
  • New Topic

Stateless Object  RSS feed

 
Paul Caudle
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • 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
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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?
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
LOL
 
Sameer Srivastava Varanasi
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • 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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!