Win a copy of React Cookbook: Recipes for Mastering the React Framework this week in the HTML Pages with CSS and JavaScript forum!
  • 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
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

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: 23906
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.
 
I am mighty! And this is a mighty small ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic