0

I am trying to figure out how to access the sb variable in the void ref method. Is it possible? This problem came up as I prepare for a test.

public class X{
  public void ref(String refStr, StringBuilder refSB){
    refStr = refStr + refSB.toString();
    refSB.append(refStr);
    refStr = null;
    refSB = null;
    //how can I access main variable sb here?  Not possible?
    //sb.append(str);
  }
  public static void main(String[] args){
    String s = "myString";
    StringBuilder sb = new StringBuilder("-myStringBuilder-");
    new X().ref(s, sb);
    System.out.println("s="+s+" sb="+sb);
  }
}
3
  • 5
    sb is refSB in your method. Commented May 27, 2013 at 12:14
  • It should either be passed as a parameter (as it is now), or be a global variable. Commented May 27, 2013 at 12:15
  • @sashkello global variables? please, let's not go there... Commented May 27, 2013 at 12:39

2 Answers 2

2

You're assigning a null to the reference value, this makes it point nowhere. When you pass a parameter, you're passing it by reference (memory pointer). Assigning a new value or null changes the reference, but not the memory object it points to.

So, you can work with the StringBuilder within the method and it will keep your changes outside the method, but you cannot assign something else to the pointer (because the pointer itself is local to the method).

For example:

public static void ref (StringBuilder refSB) {
  refSB.append("addedWithinRefMethod");  // This works, you're using the object passed by ref
  refSB = null;  // This will not work because you're changing the pointer, not the actual object
}

public static void main(String[] args) {
  StringBuilder sb = new StringBuilder();
  ref(sb);
  System.out.println(sb.toString());  // Will print "addedWithinRefMethod".
}

To make the code do what you want, you need to use one more derreference, for example using an array:

public static void ref(StringBuilder[] refSB) {
  refSB[0] = null;  // This works, outside the method the value will still be null
}

public static void main(String[] args) {
  StringBuilder[] sb = new StringBuilder[] { new StringBuilder() };
  ref(sb);
  System.out.println(sb[0]);  // Will print "null"
}

However, keep in mind that side effects (a method that changes the objects defined outside it) is generally considered bad practice and avoided when possible.

Sign up to request clarification or add additional context in comments.

Comments

1

Yes,it is possible to use sb reference in void ref() method.You are actually passing sb reference to ref() using new X().ref(s, sb); And in

 public void ref(String refStr, StringBuilder refSB){
    refStr = refStr + refSB.toString();
    refSB.append(refStr);
    refStr = null;

    //USe refSB variable here
    refSB.append(str);
  }

Don't do this refSB = null;.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.