File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What is meant by state ful object and stateless object?

 
jacob deiter
Ranch Hand
Posts: 580
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is meant by state ful object and stateless object?
 
Campbell Ritchie
Sheriff
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
web page. It appears stateless objects have no fields to record state in, but stateful objects do have fields.
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Campbell Ritchie:
web page. It appears stateless objects have no fields to record state in, but stateful objects do have fields.


I think that page presents a false, or at least incomplete, dichotomy. According to the two definitions on that page, which category would a class like String fall into?
 
Campbell Ritchie
Sheriff
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are right. Incomplete. It doesn't allow for immutable objects like Strings; I would say that a String is stateful even though its state doesn't change.

I have also thought since I found that link, a stateless object ought not to exists; if there are no fields why instantiate the class at all? Why not use an uninstantiable class and static methods?
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have also thought since I found that link, a stateless object ought not to exists; if there are no fields why instantiate the class at all? Why not use an uninstantiable class and static methods?


I had to think about this question for a while before I came up with an answer that satisfied me. I guess the answer, and possibly the answer to the OP's question, is that an uninstantiable class with only static methods cannot be replaced with a different implementation. A simple example would be the Comparator interface. Most Comparators have no instance fields, they operate only on the arguments to the compare() method, therefore most Comparator implementations are stateless by any definition. However, for obvious reasons it wouldn't be useful for there to be a single Comparator with a static compare() method.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That does provide an example of where a stateless object is useful. That example however stems from the design of the language using interfaces. If it were possible to override static methods, it would be conceivable to use a Comparator<T> with only a static compare(T, T) method.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic