aspose file tools*
The moose likes Beginning Java and the fly likes object and its state Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "object and its state" Watch "object and its state" New topic
Author

object and its state

dexter.deepak mishra
Greenhorn

Joined: Jun 19, 2008
Posts: 2
why make these two terms ..an object was enough!! this sounds a bit strange but if the state of an object changes ...it is a new object by itself ..right??

suppose "bill" is an instance of class "humans". now "bill at age of 5" is an entirely different object than "bill at the age of 50"....but what we are taught in the oop concepts are that bill at age 5 and 50 are 2 different states of the same object.

i feel my point of argument to be stronger ...but am just a noooob to say so.
i would want some expert comments ..please !!
Keith Nagle
Ranch Hand

Joined: May 06, 2008
Posts: 65
Hi.
No. If an objects state changes, it is not (typically) a new object or different object.
It is still the same object on the heap, at the same location.
You have just changed its state.
If Bill's status changes from employed = false;
to employed = true; its still the same Bill (Instance) except
his state has changed. Even if you have something like
final StringBuilder sb = new StringBuilder("hi");
this objects state can change although it's reference cannot refer
to a different StringBuilder instance.
Paradoxically, there are Strings, which are exception to the
rule, per se.
Strings are immutable. Their state cannot change.
So if you say
String s = "hello";
s = "Goodbye";

s now refers to a NEW String instance, yet the original strings
state has not changed. It cannot, because String is final.
Go figure!

[ June 19, 2008: Message edited by: Keith Nagle ]
[ June 19, 2008: Message edited by: Keith Nagle ]

SCJP 5.0
Ashish Hareet
Ranch Hand

Joined: Jul 14, 2001
Posts: 375
Hi,

Give this a thought

* X is born & issued a birth certificate
* X collects knowledge from various sources, school, college, etc
* X imparts his/her knowledge to the resources of the employer

Does X get a new birth certificate everytime X transitions in life. It's the same with objects. Lossely speaking, with OOP we try to emulate how humans perceive things & interact.

Lets for example say that X is a search engine bot

* X was initialized(born) & got memory allocated on the heap(issued a birth certificate)
* X starts crawling the web & collects information(knowledge)
* X goes back to it's caller(employer) & reports collected information(imparts knowledge)

In this example above, much like in real life, can we really say that X is a new object at the different stages of its transitions ?

To quote your example, in real life, if bill at age 5 is not the same as bill at age 50 then there is a fundamental disconnect with reality.

Now if you tell me that you'd like to consider a single atom at every point in the spacetime continuum as unique instances then that would be a philosphical argument
dexter.deepak mishra
Greenhorn

Joined: Jun 19, 2008
Posts: 2
thanks a lot to both of you !!
i was indeed being somewhat "metaphysical".
but still i cant "get a real picture" of the "in-memory" transformation of the mutable objects.
i had actually never cared for the difference between the mutable and immutable objects...just remembered their definitions...but now i am looking for some better concepts.
i hope you people will help here too.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: object and its state
 
Similar Threads
State Pattern
need some explanation
Regarding Design Patterns
Confusion regarding stateful session bean
Slider bar - Slider from both ends