File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "object and its state" Watch "object and its state" New topic

object and its state

dexter.deepak mishra

Joined: Jun 19, 2008
Posts: 2
why make these two terms object was enough!! this sounds a bit strange but if the state of an object changes 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
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

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

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:
subject: object and its state
It's not a secret anymore!