Results 1 to 6 of 6
  1. #1
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Arrow How to change value by reference?

    Here is an example code:

    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
        }
    }
    how can i get someString to become "IWantThisString" and print that out?
    Im trying to get it work for another project
    Restrictions are that I use that boolean function.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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.html
    Last edited by sunde887; 05-08-2011 at 05:16 AM.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    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.

  4. #4
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default 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

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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.

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

Similar Threads

  1. object and reference
    By aizen92 in forum New To Java
    Replies: 11
    Last Post: 04-01-2011, 08:39 PM
  2. Reference variable
    By lala in forum New To Java
    Replies: 2
    Last Post: 02-03-2011, 07:27 PM
  3. persistent of same reference twice
    By smackdown90 in forum Web Frameworks
    Replies: 0
    Last Post: 04-25-2010, 06:53 PM
  4. Don't pass by reference
    By Lyven in forum Advanced Java
    Replies: 6
    Last Post: 11-16-2009, 06:06 PM
  5. Getting the Object Reference Name
    By Deathmonger in forum New To Java
    Replies: 2
    Last Post: 03-12-2008, 02:51 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •