• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Temporary objects

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...

https://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
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic