Win a copy of Java 9 Revealed this week in the Features new in Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Final local variable cannot be assigned error  RSS feed

 
Kevin Farmer
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm creating a JFrame that displays information and then erases the information when "New" is pressed.

I have these variables initialized in the beginning of the class.

And when I try to clear the data variable it tells me "The final local variable data cannot be assigned, since it is defined in an enclosing type".

I'm not sure where to fix this error.
 
Norm Radder
Rancher
Posts: 1772
26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried removing "final"?  final is for  variables that you don't want changed?

If you just want to change the contents of the array that can be done with code in a loop.
 
Kevin Farmer
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I removed the final and said "Local variable data defined in an enclosing scope must be final".

Also, the data field would have multiple lines of data. Would a loop clear all of the lines?
 
Norm Radder
Rancher
Posts: 1772
26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

"Local variable data defined in an enclosing scope

vs

have these variables initialized in the beginning of the class.

The compile thinks the variables are local but your comment implies they are defined at the class level???


Would a loop clear all of the lines?

It could assign an empty String or null values to all the elements in the array.
 
Kevin Farmer
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My mistake, they are not class level.

However, this is the for loop that I created but I am still getting the "variable must be final" error.

Shouldn't this clear the data table?
 
Norm Radder
Rancher
Posts: 1772
26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no need for a loop if the same value is assigned to the same variable on every iteration.

The assigning I was talking about was to the elements of the array, not the variable name for the array.

Use array notation ([][]) to access the elements in an array:
theArray[row][col] = value; // asssign a value to an element in theArray

For a 2 dim array, you would need a nested loop
 
Campbell Ritchie
Sheriff
Posts: 54030
130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which version of Java® are you using? The rules are slightly different in Java8
Right: a class defined locally has references to variables in its enclosing scope which includes fields and local variables. The same applies to variables used in a λ. Now, the lifetime of the instances of that local class will be longer than the time for the method to complete. So the local class needs access to some variables. If they are local variables, they will vanish from the stack memory while the local object still exists. So the local class takes a copy of those variables. And the other variable is assigned to and there are now two different copies. No, that last bit won't work. You can't have two different copies in the two locations. So you are not allowed to reassign the local variable.
You have declared data as a 1‑element array of arrays of Objects. So it must remain that 1‑element array of arrays of Object for ever, but there is nothing to stop you altering its state. See the official definition of final variables in the Java® Language Specification (=JLS). This can be difficult to read, but it tells you that the state of an object pointed to by a final reference can be changed. You can reassign the one element (an array) or any element of that array, but you cannot replace that object with a different object. That is how you can change the state of an Object[][].
data[0][2] = "CodeRanch rocks ;-)"; OK
data[1][2] = "CodeRanch rocks ;-)"; That line won't work because you originally initialised data with one element (=included array), and you cannot change it to have two elements.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!