• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

object and its state

 
dexter.deepak mishra
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ashish Hareet
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic