-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

Grammin
  • 11,808
  • 22
  • 80
  • 138
  • 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. – Gene Jul 04 '12 at 15:46
  • I've updated with the actual code I'm using. – Grammin Jul 04 '12 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. – Gene Jul 04 '12 at 15:59
  • @Gene You're not helping – Grammin Jul 04 '12 at 16:02
  • But I am. Please see my remark under the code that started working. There is some other problem. – Gene Jul 04 '12 at 17:34

3 Answers3

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();
}
Simon Dorociak
  • 33,374
  • 10
  • 68
  • 106
  • 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 ( + )". – hatchet - done with SOverflow Jul 04 '12 at 15:54
  • 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 – Grammin Jul 04 '12 at 15:55
  • see http://stackoverflow.com/questions/1532461/stringbuilder-vs-string-concatenation-in-tostring-in-java and http://stackoverflow.com/questions/4648607/stringbuilder-stringbuffer-vs-operator ...they are essentially the same – hatchet - done with SOverflow Jul 04 '12 at 16:01
  • 1
    Thank you for helping me answer the question, instead of blinding just asking for more more more stuff that isn't relevant. – Grammin Jul 04 '12 at 16:05
1

What worked:

  public void setMambaPath(String executable)
  {

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

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

    execpath_ = executable;
  }
Grammin
  • 11,808
  • 22
  • 80
  • 138
  • 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. – Andrew Lazarus Jul 04 '12 at 16:45
  • 1
    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 Jul 04 '12 at 17:33
  • @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. – hatchet - done with SOverflow Jul 04 '12 at 21:14
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.

tsm
  • 3,598
  • 2
  • 21
  • 35
  • This is all the code, and I can look at it in the debugger after the foo call and it hasn't changed. – Grammin Jul 04 '12 at 15:46