# Final-keyword doesn't work properly in Pipeline

**URL:** <https://community.jenkins.io/t/final-keyword-doesnt-work-properly-in-pipeline/33172>\
**Category:** Using Jenkins\
**Tags:** pipeline\
**Created:** [June 24, 2025, 9:38am UTC](https://community.jenkins.io/t/final-keyword-doesnt-work-properly-in-pipeline/33172 "2025-06-24T09:38:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![pyatizbyantsevia](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/pyatizbyantsevia/32/11941_2.png) [@pyatizbyantsevia](https://community.jenkins.io/u/pyatizbyantsevia)\
**Post date:** [June 24, 2025, 9:38am UTC](https://community.jenkins.io/t/final-keyword-doesnt-work-properly-in-pipeline/33172/1 "2025-06-24T09:38:54Z")

</div>

I want to use a constant in my pipeline:

```java
pipeline {
    agent any

    stages {
        stage('Hello') {
            steps {
                script {
                    final String str = "someVal"
                    str = "yetAnotherVal" // Exception expected
                    echo str
                }
            }
        }
    }
}

```

But this pipeline is executed successfully, I get output in stdout: “yetAnotherVal”

I went ahead and simply ran the following code in Main Pipeline Script via Replay:

```auto
final String str = "someVal"
str = "yetAnotherVal" // Exception expected
println str

```

The result is the same

Is this related to Jenkins using Groovy 2.4.21?

I assume so, because Groovy 2.4.21 also has this behavior:

```auto
ilyait@DESKTOP-H4GIJH3:/mnt/c/Users/ilya/Documents$ cat main.groovy
final String str = "someVal"
str = "1"
println str
ilyait@DESKTOP-H4GIJH3:/mnt/c/Users/ilya/Documents$ ./groovy-2.4.21/bin/groovy main.groovy
1

```

This is fixed in Groovy 2.5.10:

```auto
ilyait@DESKTOP-H4GIJH3:/mnt/c/Users/ilya/Documents$ ./groovy-2.5.10/bin/groovy main.groovy
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
/mnt/c/Users/ilya/Documents/main.groovy: 2: The variable [str] is declared final but is reassigned
. At [2:1] @ line 2, column 1.
   str = "1"
   ^

1 error

```

If this is indeed related to an outdated version of Groovy, what stage is this issue at now?

* * *

My Jenkins setup:

Jenkins: 2.492.2  
OS: Linux - 5.15.167.4-microsoft-standard-WSL2  
Java: 17.0.14 - Eclipse Adoptium (OpenJDK 64-Bit Server VM)  
[installed-plugins.log](https://community.jenkins.io/uploads/short-url/jwTdEwdXKCJne8JPoBxzRLbsAT0.log) (2.9 KB)

---

<div class="post-metadata">

**Author:** ![slide\_o\_mix](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/slide_o_mix/32/20977_2.png) [@slide\_o\_mix](https://community.jenkins.io/u/slide_o_mix)\
**Post date:** [June 24, 2025, 6:47pm UTC](https://community.jenkins.io/t/final-keyword-doesnt-work-properly-in-pipeline/33172/2 "2025-06-24T18:47:54Z")

</div>

Yes, you are most likely hitting this because Jenkins is using a version of Groovy that doesn’t work for final in the way you are expecting. Updating Groovy is a HUGE undertaking, that I am not sure is on the roadmap yet, but I could be wrong.

---

<div class="post-metadata">

**Author:** ![pyatizbyantsevia](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/pyatizbyantsevia/32/11941_2.png) [@pyatizbyantsevia](https://community.jenkins.io/u/pyatizbyantsevia)\
**Post date:** [June 25, 2025, 8:48am UTC](https://community.jenkins.io/t/final-keyword-doesnt-work-properly-in-pipeline/33172/3 "2025-06-25T08:48:07Z")

</div>

Thanks for the reply!)

Can the community help advance this complex task? If there are open issues I would be happy to see what I can do

---

<div class="post-metadata">

**Author:** ![mawinter69](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/mawinter69/32/1625_2.png) [@mawinter69](https://community.jenkins.io/u/mawinter69)\
**Post date:** [June 25, 2025, 10:06am UTC](https://community.jenkins.io/t/final-keyword-doesnt-work-properly-in-pipeline/33172/4 "2025-06-25T10:06:23Z")

</div>

[https://issues.jenkins.io/browse/JENKINS-53372](https://issues.jenkins.io/browse/JENKINS-53372) is the first ticket for upgrading from 2.4 to 2.5  
The biggest problem seems to be the sandboxing done in Jenkins

---

<div class="post-metadata">

**Author:** ![jglick](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/jglick/32/77_2.png) [@jglick](https://community.jenkins.io/u/jglick)\
**Post date:** [June 26, 2025, 10:52am UTC](https://community.jenkins.io/t/final-keyword-doesnt-work-properly-in-pipeline/33172/5 "2025-06-26T10:52:02Z")

</div>

Updating Groovy would not likely fix this. Pipeline script is run through a Jenkins-specific transformer which winds up essentially running an independent interpreter, and I suppose nobody bothered to check for the `final` modifier in this context. Just pretend the modifier does not exist and do not assign another value.
