Details
-
Type:
Sub-task
-
Status:
Open
-
Priority:
Major
-
Resolution: Unresolved
-
Affects Version/s: None
-
Fix Version/s: None
-
Component/s: None
-
Labels:None
-
Environment:Ubuntu 6.10 Edgy Eft + Groovy r4630
-
Number of attachments :
Description
The following code appears to show that final is being applied inconsistently. A final list can be amended but an object that manipulates a lsit that is final cannot. In the former case the final is being applied to the reference and in the later, it is being applied to the object.
class Blah {
def list = []
public plus ( item ) {
list += [ item ]
return this
}
}
class Foobar {
final static blah = new Blah ( )
}
final x = []
x += [1]
println ( x )
Foobar.blah += 1
println ( Foobar.blah.list )
| > groovy finalProblem.groovy [1] Caught: java.lang.IllegalAccessException: Field is final at finalProblem.run(finalProblem.groovy:17) at finalProblem.main(finalProblem.groovy) |
Issue Links
- is duplicated by
-
GROOVY-2752
final keyword does not work
-
-
GROOVY-1416
final modifier ignored in scripts
-
- is related to
-
GROOVY-3088
Modifiers on local variables are ignored
-
- relates to
-
GROOVY-1475
final modifier on a method parameter is not respected
-
-
GROOVY-3088
Modifiers on local variables are ignored
-
You get a runtime error, is that not OK for you?
The problem is, that we could provide a compile time error here, but only because a static field is used. The compiler might not be able to ensure this for fields used on instances.