-1

I have the following code:

private String foo;

public void setFoo(String bar)
{
  foo = bar + "bin/";
}

I expect this code to concat bar and "bin/" using the overloaded '+' operator. And when I do this same code sample in the debugger it works fine. For some reason though foo is always just equal to bar and never has the "bin/" in it.

Actual code:

 private String execpath_;

  public void setMambaPath(String executable)
  {

    if (!(executable.endsWith("/")))
      executable = executable.concat("/");

    execpath_ = executable + "bin/";
  }

elsewhere where execpath_ = just excutable without the bin/:

StringBuilder cmd = getSshCommand_();
cmd.append(execpath_ + "mambaService");

I don't use execpath_ anywhere else

5
  • 1
    Calling setFoo will set foo to bar with bin/ concatenated. There is something wrong in the rest of your code or environment. Since you haven't posted it, no one can tell you what that is. Commented Jul 4, 2012 at 15:46
  • I've updated with the actual code I'm using. Commented Jul 4, 2012 at 15:50
  • 1
    Still not enough code. When you show us where there is a Log trace message with the value you don't want, we'll be in business. Commented Jul 4, 2012 at 15:59
  • @Gene You're not helping Commented Jul 4, 2012 at 16:02
  • But I am. Please see my remark under the code that started working. There is some other problem. Commented Jul 4, 2012 at 17:34

3 Answers 3

3

String is immutable variable and does not contain methods that change the content of the String object itself. So you need to use concat() method.

Or second approach you can use StringBuilder

private String foo;

public void setFoo(String bar)
{
  StringBuilder builder = new StringBuilder();
  builder.append(bar + "bin/");
  foo = builder.toString();
}
Sign up to request clarification or add additional context in comments.

4 Comments

He cannot change the string in-place, but surely he can replace the string his variable references (which is what he's doing). And + seems a fine operator to use..."The Java language provides special support for the string concatenation operator ( + )".
execpath_ = executable.concat("bin/"); Doesn't change it also. I'm so confused I'm literally stepping through it in the debugger, it doesn't make any sense
Thank you for helping me answer the question, instead of blinding just asking for more more more stuff that isn't relevant.
1

What worked:

  public void setMambaPath(String executable)
  {

    if (!(executable.endsWith("/")))
      executable = executable.concat("/");

    executable = executable.concat("bin/");

    execpath_ = executable;
  }

3 Comments

So at least two unnecessary temporary Strings. The StringBuilder approach is significantly more efficient. Nor would it surprise me if the execpath_ field should be made immutable once set. Immutable objects are good.
It still doesn't make any sense that this works when the original doesn't. It can only be 1) something else changed (as there was a build problem that resolved itself), 2) a compiler bug or else 3) one thread is the writer and another is the reader, so the member must be declared volatile to ensure a thread local copy isn't made. If it's the thread local problem, then it could have gone away merely because the call to concat() discouraged the optimizer. Not a robust solution. This is why I keep asking for more code. There is still not enough to solve this problem.
@Gene - I have to agree. I think the 'fix' is a fix by side effect. The root cause may very well still be there, just less likely to occur (but still may occur). The symptoms fit a threading issue going on.
0

Post more code (including where you use foo later). Either foo is getting modified somewhere else, or setFoo has a local foo that's being modified instead of this.foo. I'm pretty sure it's the first one though.

1 Comment

This is all the code, and I can look at it in the debugger after the foo call and it hasn't changed.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.