How to create and access global variables in Groovy?

I need to store a value in a variable with one method, and then I need to use this value from this variable in another method or closure. How can I share this value?

+61
groovy
Jun 10 '11 at 11:44
source share
8 answers

In the Groovy script, the scope may be different than expected. This is because the Groovy script itself is a class with a method that will run the code, but this is all done at runtime. We can define a variable that will be bound to the script, either by 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. } 
+122
Jun 11 '11 at 11:27
source share
 class Globals { static String ouch = "I'm global.." } println Globals.ouch 
+28
Jun 10 '11 at 11:53 on
source share

Just declare a variable in the class or scope of the script, then access it from your methods or closures. Without an example, it is difficult to be more specific to your specific problem.

However, global variables are generally considered bad.

Why not return a variable from one function and then pass it to the next?

+2
Jun 10 '11 at 11:50
source share

Like all OO languages, Groovy does not have the concept of "global" in itself (unlike, say, BASIC, Python or Perl).

If you have several methods that should use the same variable, use the field:

 class Foo { def a; def foo() { a = 1; } def bar() { print a; } } 
+1
Jun 10 '11 at 11:50
source share

I think you are talking about class level variables. As mentioned above, using global level / class variables is not good practice.

If you really want to use it. and if you are sure that there will be no impact ...

Declare any variable outside the method. class level variable type

eg:

 { method() { a=10 print(a) } // def a or int a wont work a=0 } 
+1
Jul 04 '13 at 20:59 on
source share
 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
Dec 30 '15 at 23:57
source share

Failed to figure out what you want, but do 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 of a . Now you can do something with bValue Let me know if I misunderstood your question.

0
Jun 11 '11 at 7:23
source share
 def iamnotglobal=100 // This will not be accessible inside the function iamglobal=200 // this is global and will be even available inside the def func() { log.info "My value is 200. Here you see " + iamglobal iamglobal=400 //log.info "if you uncomment me you will get error. Since iamnotglobal cant be printed here " + iamnotglobal } def func2() { log.info "My value was changed inside func to 400 . Here it is = " + iamglobal } func() func2() 

here iamglobal variable is a global variable used by func and then again accessible to func2

0
Sep 01 '17 at 6:12
source share



All Articles