*
The moose likes Beginning Java and the fly likes What is meant by state ful object and stateless object? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What is meant by state ful object and stateless object?" Watch "What is meant by state ful object and stateless object?" New topic
Author

What is meant by state ful object and stateless object?

jacob deiter
Ranch Hand

Joined: Apr 02, 2008
Posts: 576
What is meant by state ful object and stateless object?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38441
    
  23
web page. It appears stateless objects have no fields to record state in, but stateful objects do have fields.
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
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?


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38441
    
  23
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

Joined: Jan 17, 2006
Posts: 1296
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

Joined: Oct 13, 2005
Posts: 38441
    
  23
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.
 
Don't get me started about those stupid light bulbs.
 
subject: What is meant by state ful object and stateless object?