• Post Reply Bookmark Topic Watch Topic
  • New Topic

private method for changing the string instance  RSS feed

 
vojin djukic
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, does anyone have idea why this doesnt work. I want to make a private method that will change the value of a instance variable like this:


String bla = "bla bla bla";

private void changeString (String x) { x = "eee";}

changeString(bla);


Now value of the variable bla should be "eee" but its not, it is stil "bla bla bla".
 
Campbell Ritchie
Marshal
Posts: 56553
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

It has to do with pass‑by‑value. The String you have inside the method is a local copy of the reference passed. When you change the reference inside the method, nothing happens to the original. There is more about it here but you will take a long time reading that thread.
 
Jude Niroshan
Ranch Hand
Posts: 132
5
Eclipse IDE Java Postgres Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi vojin,
Welcome to Ranch. Please read this article. This will clear all your doubts and definitely give you the answer for your question.
Regards,
Jude
 
vojin djukic
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks! Articles were very helpful.
 
Campbell Ritchie
Marshal
Posts: 56553
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!