Results 1 to 6 of 6
- 05-08-2011, 05:00 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 4
- Rep Power
- 0
How to change value by reference?
Here is an example code:
how can i get someString to become "IWantThisString" and print that out?Java Code:package test; public class Test { static boolean test (String someString) { boolean anotherBool = true; someString = "IWantThisString"; return anotherBool; } public static void main(String[] args) { String someString = "NotThisString"; boolean someBool = false; someBool = test(someString); System.out.println(someBool + " " + someString); //It prints the following: //true NotThisString } }
Im trying to get it work for another project
Restrictions are that I use that boolean function.
- 05-08-2011, 05:11 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
I'm not sure what you want is really possible. I could be wrong and I will learn something if I am.
If you have an instance variable you can have the method change the variable.
As far as I know you are passing in a copy to the method and I don't believe you can pass in a reference.
Check this link out http://www.javaworld.com/javaworld/j...0526-pass.htmlLast edited by sunde887; 05-08-2011 at 05:16 AM.
-
Strings are immutable so what you are trying to do is impossible. It will work if you use a static class variable.
Java Code:public class Test { static String otherString = "Not this String"; static boolean test(String someString) { boolean anotherBool = true; someString = "IWantThisString"; otherString = "I want this String"; return anotherBool; } public static void main(String[] args) { String someString = "NotThisString"; boolean someBool = false; someBool = test(someString); System.out.println(someBool + " " + someString); System.out.println(otherString); // It prints the following: // true NotThisString } }Last edited by Fubarable; 05-08-2011 at 05:15 AM.
- 05-08-2011, 05:16 AM #4
Member
- Join Date
- Mar 2011
- Posts
- 4
- Rep Power
- 0
port to c++?
edit:
ahh you guys edit your posts too fast.
thanks for the linky sunde887
and thanks fubarable, i will look into that.Last edited by chris83190@hotmail.com; 05-08-2011 at 05:22 AM. Reason: edit
- 05-08-2011, 07:43 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Restrictions are that I use that boolean function.
??? But you've just seen that that boolean method does not do what you want.
There's yucky: have a static variable or pass an array or an instance of some contrived class.
And there's nice: return the string and assign it to someString.
--------------
If you use a variable as a parameter in some method call then that variable will have the same value after the method has finished. Every variable, every time. That's just how Java works.
If you want to change the value a variable has you assign something to it. (Including, in this case, the thing returned by a method.)
-------------
I'm not sure the immutability of strings has anything to do with this.
- 05-08-2011, 07:52 AM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Since it's been raised, here's a discussion of passing, and values:
JavaRanch Campfire - Cup Size: a Story About Variables
JavaRanch Campfire - Pass By Value, Please
Similar Threads
-
object and reference
By aizen92 in forum New To JavaReplies: 11Last Post: 04-01-2011, 08:39 PM -
Reference variable
By lala in forum New To JavaReplies: 2Last Post: 02-03-2011, 07:27 PM -
persistent of same reference twice
By smackdown90 in forum Web FrameworksReplies: 0Last Post: 04-25-2010, 06:53 PM -
Don't pass by reference
By Lyven in forum Advanced JavaReplies: 6Last Post: 11-16-2009, 06:06 PM -
Getting the Object Reference Name
By Deathmonger in forum New To JavaReplies: 2Last Post: 03-12-2008, 02:51 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks