aspose file tools*
The moose likes Java in General and the fly likes Temporary objects 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 » Java in General
Bookmark "Temporary objects" Watch "Temporary objects" New topic
Author

Temporary objects

sireesh Don
Greenhorn

Joined: Mar 01, 2010
Posts: 3
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
Sheriff

Joined: Sep 28, 2004
Posts: 18133
    
  39


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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3056
    
  33

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.


Mohamed Sanaulla | My Blog
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Temporary objects
 
Similar Threads
Problem in implementing a TreeSet
Please Explain (Hashcode)
toString() and null
Help in writing a better program for Vampire numbers
ThreadUnsafe