0

My program reads data from socket and now I want to display that data in a textbox. I splitted the data into seperate variables and here is my code:

                  final int aData = 0;
                  final int aData = 0;
                  final int cData = 0;

                      final String[] separated = data.split(":");
                      if ((separated.length == 3) && (data.contains(":")))
                      {
                            aData = Integer.parseInt(separated[0]);
                            bData   = Integer.parseInt(separated[1]);
                            cData     = Integer.parseInt(separated[2]);


                      }

                      handler.post(new Runnable() {
                            public void run() {
                                    txtDebug.setText("a: "+aData + " b: "+bData + " c: " + cData);

                            }
                          });

it doesn't allow me to run the program and shows me following error:

"The final local variable aData cannot be assigned. It must be blank and not using a compound assignment". Any help to solve the problem would be highly appreciated.

MKS
  • 736
  • 2
  • 14
  • 33
  • possible duplicate of [Why would one mark local variables and method parameters as "final" in Java?](http://stackoverflow.com/questions/316352/why-would-one-mark-local-variables-and-method-parameters-as-final-in-java) – Simon Mar 04 '14 at 21:42

3 Answers3

0

You're assigning aData twice. I assume you meant to have aData, bData and cData but you have two lots of aData and one cData.

Besides that problem, you also assign 0 to each of these variables when you declare them. You then try to assign new values in the if block. You can only assign to a final variable once.

SDJMcHattie
  • 1,690
  • 1
  • 15
  • 21
  • That's because you then refer to the variables in the Runnable which is another class. If you need to access those variables inside there, you should reassign the values to new variables before you declare the Runnable. Something like `final int finalA = aData; final int finalB = bData;` then use `finalA` and `finalB` in the Runnable. – SDJMcHattie Mar 04 '14 at 21:42
  • is it possible to declear the variable outside the Runnable and display the results in the textbox? – MKS Mar 04 '14 at 21:44
  • Yes, on the line just before `handler.post(new Runnable() {` reassign the variable like I said. `final int finalAData = aData;` will put the value of aData in a final variable called `finalAData` which you can access inside the Runnable. – SDJMcHattie Mar 04 '14 at 21:48
  • 1
    Or, put the whole `handler.post()` call inside a new method `private void updateDebugTextOnMainThread(final int aData, final int bData, final int cData) {` and call it directly with your non-final `aData`, `bData` and `cData` – SDJMcHattie Mar 04 '14 at 21:50
  • Or another option is, produce a final String outside the Runnable with your debug text: `final String debugText = "a: "+aData + " b: "+bData + " c: " + cData;` and inside the Runnable just do `txtDebug.setText(debugText);` – SDJMcHattie Mar 04 '14 at 21:53
0

The final reserved work means that your variable might be initialized/assigned just once. Since you're assigning final int aData = 0;, it means it won't be able to assign it again. This has its benefits (for instance, if you're sure you just want your variable be assigned for once, you should declare it as final - even this will help a little to the compiler), but its for sure a behavior you're not looking for. Simply remove final from your variables.

nKn
  • 13,691
  • 9
  • 45
  • 62
  • I removed final and it shows following error in the txtDebug "Cannot refer to a non-final variable aData inside an inner class defined in a different method" – MKS Mar 04 '14 at 21:40
  • 1
    Then you'll have to use a workaround. Define a `int[] data = new int[3]` and declare it as `final`. Use the first index for `aData`, the second for `bData` and the third for `cData`. This way it will allow you to modify the datastructure as it's not basic. – nKn Mar 04 '14 at 21:44
0

Since you have used the keyword 'final' on the variables aData, bData and cData, they can no longer be reassigned new values elsewhere within the program. They take the value 0 and remain that way. Remove the modifier 'final' if you want to change them.

ucsunil
  • 7,378
  • 1
  • 27
  • 32
  • I removed final and it shows following error in the txtDebug "Cannot refer to a non-final variable aData inside an inner class defined in a different method" – MKS Mar 04 '14 at 21:41
  • Make it a member variable (declare it outside of a method) without the `final` modifier. That will allow you to access it anywhere in the class. – codeMagic Mar 04 '14 at 21:42
  • I wouldn't recommend making a variable effectively global as a field variable just because you need to access it from the Runnable. That's bad software engineering. Only use field variables if you need to have access to the variable from several methods in the class and for properties of the class. – SDJMcHattie Mar 04 '14 at 21:46
  • @SDJMcHattie I have no idea how often the OP needs the variable so it's a suggestion that will work and can be used in other places. Guess I should have made that disclaimer – codeMagic Mar 04 '14 at 21:55