Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Temporary objects

 
sireesh Don
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I have one doubt on temporary objects ...




in the above program when is set

t3.setVal("sdfsa"); ---> if t3 value set why t1 , t2 object values setting according to t3.
in this program what i have to do even though t3 value set , it should not update the t1,t2 objects
i have to have t2 value in t3.

it is some thing like my main object should remain same .. i want to do some operations on temporary object which was get assigned from main object...
becoz of temporary object my main object should not change..


i am sorry if iam not explain the problem clearly


your help is highly appricated..

many thanks in advance
 
Henry Wong
author
Marshal
Pie
Posts: 20892
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

First, you need to understand what is a local variable. Please review your book on the subject.

Second, see this about parameter passing...

http://www.coderanch.com/how-to/java/CallByReferenceVsCallByValue


Anyway, the answer is, you need to understand the concept of references and objects -- how object references are passed around, and what happens when you assign one reference to another, and why you new test1() calls aren't really working the way you want it to.

Henry
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sireesh Don wrote:
...
t3.setVal("sdfsa"); ---> if t3 value set why t1 , t2 object values setting according to t3.
in this program what i have to do even though t3 value set , it should not update the t1,t2 objects
i have to have t2 value in t3.

it is some thing like my main object should remain same .. i want to do some operations on temporary object which was get assigned from main object...
becoz of temporary object my main object should not change..
...


You see- t1,t2,t3 all point to the same instance. In java its pass by value. So when you are passing around the reference variables through the methods- they would all refer to the same instance on the heap- the reason is simple- The value which is stored is the address of the instance and gets copied to multiple references. As Henry has mentioned- the link explains this pretty well.

Coming to your temporary object- You can use clone(). Read more about it from the API- Cloneable. But you make changes in your logic to actually avoid this kind of dependency.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic