This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin for Android App Development and have Peter Sommerhoff on-line!
See this thread for details.
Win a copy of Kotlin for Android App Development this week in the Kotlin 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

set method and references with Strings  RSS feed

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd like to know what happens behind the scenes with this call in main and this settor. So in main a have some object of class Vehicle and i want to set its name i also have some strings.





Now here's my doubt. When i do ; honda gets the name civic. if i then in main do s1 = s2, s1 now points to "concerto" but the honda object still prints civic. I thought when i did setName the variable was pointing to s1 but it seems that instead it does a copy?
 
Bartender
Posts: 3321
86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
s1 holds a reference to the string "civic". When you call setName passing in String s1 you pass the value of the reference which you then assign to 'name'. 'name' now holds a reference to "civic", its not pointing to s1 and it's not holding a copy of "civic" so when you assign s2 to s1 'name' is not affected at all.
 
Bartender
Posts: 10750
68
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Chris Yates wrote:I'd like to know what happens behind the scenes with this call in main and this settor.


I think Tony's basically covered it; but you might want to think about whether you actually want a "setter".

Can a Honda Civic suddenly "become" a Honda Accord? It seems unlikely to me, but your setter method allows precisely that to happen.

A good rule for the future: Getters: good; Setters (in general): bad - so think carefully before you add them.

HIH

Winston
 
Chris Yates
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That was just to show my doubts. Now i know that after calling a settor with a string in main function changing that string later won't change the object because the field in the object still points to the string used in the settor. There was some confusion in my head but now it's all clear, thanks for help.
 
Rancher
Posts: 795
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chris, from your example and your comments there still might be confusion. What you are asking about is basic life, scope, and visibility. It is not that easy of concept to illustrate, but there is a small code example that I hope will do the job for you:
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!