I need to store a value in a variable in one method and then I need to use that value from that variable in another method or closure. How can I share this value?
8 Answers
In a Groovy script the scoping can be different than expected. That is because a Groovy script in itself is a class with a method that will run the code, but that is all done runtime. We can define a variable to be scoped to the script by either omitting the type definition or in Groovy 1.8 we can add the @Field
annotation.
import groovy.transform.Field
var1 = 'var1'
@Field String var2 = 'var2'
def var3 = 'var3'
void printVars() {
println var1
println var2
println var3 // This won't work, because not in script scope.
}
-
38Just to note Field @requires an import.. import groovy.transform.Field – khylo Jan 09 '13 at 10:43
-
funny tried to edit to change then to than in the first line, SO wants a six-char edit! – JimLohse Jan 15 '16 at 18:50
-
1if you're trying to use a global variable from within a class definition, it gets a bit tricky. The `class Globals` solution below is a way around that. – solstice333 Apr 03 '17 at 21:50
-
8I tried the `var1 = 'var1'` method in a Jenkins Pipeline using Groovy and it didn't work. I had to use `@Field var1 = 'var1'` – retsigam Aug 02 '18 at 16:55
def i_am_not_global = 100 // This will not be accessible inside the function
i_am_global = 200 // this is global and will be even available inside the
def func()
{
log.info "My value is 200. Here you see " + i_am_global
i_am_global = 400
//log.info "if you uncomment me you will get error. Since i_am_not_global cant be printed here " + i_am_not_global
}
def func2()
{
log.info "My value was changed inside func to 400 . Here it is = " + i_am_global
}
func()
func2()
here i_am_global
variable is a global variable used by func
and then again available to func2
if you declare variable with def
it will be local, if you don't use def its global

- 5,141
- 5
- 38
- 59

- 3,423
- 2
- 29
- 38
class Globals {
static String ouch = "I'm global.."
}
println Globals.ouch

- 185,044
- 174
- 569
- 824

- 9,458
- 10
- 38
- 45
Like all OO languages, Groovy has no concept of "global" by itself (unlike, say, BASIC, Python or Perl).
If you have several methods that need to share the same variable, use a field:
class Foo {
def a;
def foo() {
a = 1;
}
def bar() {
print a;
}
}

- 321,842
- 108
- 597
- 820
I think you are talking about class level variables. As mentioned above using global variable/class level variables are not a good practice.
If you really want to use it. and if you are sure that there will not be impact...
Declare any variable out side the method. at the class level with out the variable type
eg:
{
method()
{
a=10
print(a)
}
// def a or int a wont work
a=0
}

- 6,269
- 19
- 81
- 104

- 11
- 1
def sum = 0
// This method stores a value in a global variable.
def add =
{
input1 , input2 ->
sum = input1 + input2;
}
// This method uses stored value.
def multiplySum =
{
input1 ->
return sum*input1;
}
add(1,2);
multiplySum(10);

- 1,498
- 1
- 11
- 10
Just declare the variable at class or script scope, then access it from inside your methods or closures. Without an example, it's hard to be more specific for your particular problem though.
However, global variables are generally considered bad form.
Why not return the variable from one function, then pass it into the next?

- 167,322
- 27
- 342
- 338
-
1what if i want to use the variable whose value have been assigned within a closure? E.g: i have def a = null at the beginning of the script. Now the inside a closure, the value of a = 'some string' , is assigned. I want this new value to be accessible to all other closures. thanks – OK999 Oct 26 '17 at 18:30
-
-
I ended creating a closure that returns the desired calculated value and using that where its needed – OK999 Oct 27 '17 at 13:24
Could not figure out what you want, but you need something like this ? :
def a = { b -> b = 1 }
bValue = a()
println b // prints 1
Now bValue
contains the value of b
which is a variable in the closure a
. Now you can do anything with bValue
Let me know if i have misunderstood your question

- 13,545
- 27
- 98
- 148