0

How can I update String object from void?
Now it gives me an error: The final local variable sObj cannot be assigned, since it is defined in an enclosing type.

String object = "";
String object2 = "";
String object3 = "";
String object4 = "";
String object5 = "";

digitInput(object); //update string object
digitInput(object4); // update string object4


private void digitInput(final String sObj) {
      ....
      sObj = NEW_VALUE; //With this I want to update passed object
      ....
}
mgulan
  • 795
  • 1
  • 14
  • 33
  • 1
    @DavidCAdams is correct. Is that you are looking for? If so you might want to refactor how this is behaving. Post more code or head over to programmers.stackexchange.com for more generalized answers. You might also want to brush up on some java. – cbrulak Feb 06 '14 at 20:50
  • I agree that you need to do some basic Java work. Note also that everything in Java is passed by value. To understand why your code didn't work, you need to understand `final`. – Simon Feb 06 '14 at 20:58
  • 1
    Demonstrates no or minimal understanding of language fundamentals – Ian Kemp Feb 07 '14 at 12:25

3 Answers3

2

You want to set the value of object to the value of sObj? this.object = sObj;

David C Adams
  • 1,953
  • 12
  • 12
  • The problem is that I don't know what String to update from void, so I need to pass specific String with "digitInput(object);" and then update it from void. – mgulan Feb 06 '14 at 20:54
  • Please, stop guessing. Learn the basics, then try again. `update it from void` is nonsense. `void` just indicates to the compiler that a method has no return value. – Simon Feb 06 '14 at 21:00
1

Remove the final qualifier from the method signature.

cbrulak
  • 15,436
  • 20
  • 61
  • 101
1

It seems like you are trying to reassign the value of the object using the function. The approach you are taking is strange. I would do it like:

String object = "";
String object2 = "";
String object3 = "";
String object4 = "";
String object5 = "";

object = digitInput(object); //update string object
object4 = digitInput(object4); // update string object4

// assuming you need the original object to generate the new value
private String digitInput(String sObj) {
      ....
      // do something
      ....
      return NEW_VALUE; //With this I want to update passed object
}    
keeganmccallum
  • 164
  • 1
  • 7