Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create and access the global variables in Groovy?

Tags:

groovy

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?

like image 425
srini Avatar asked Jun 10 '11 11:06

srini


People also ask

How do you set a global variable in Groovy?

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. }

How do I create a variable in Groovy?

Variables in Groovy can be defined in two ways − using the native syntax for the data type or the next is by using the def keyword. For variable definitions it is mandatory to either provide a type name explicitly or to use "def" in replacement. This is required by the Groovy parser.

How do we create a global variable?

The global Keyword Normally, when you create a variable inside a function, that variable is local, and can only be used inside that function. To create a global variable inside a function, you can use the global keyword.

What is the best way to declare and access a global variable?

The clean, reliable way to declare and define global variables is to use a header file to contain an extern declaration of the variable. The header is included by the one source file that defines the variable and by all the source files that reference the variable.


7 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.
}
like image 60
mrhaki Avatar answered Sep 21 '22 19:09

mrhaki


class Globals {
   static String ouch = "I'm global.."
}

println Globals.ouch
like image 20
Bob Herrmann Avatar answered Sep 22 '22 19:09

Bob Herrmann


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

like image 35
Gaurav Khurana Avatar answered Sep 21 '22 19:09

Gaurav Khurana


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;
    }
}
like image 39
Aaron Digulla Avatar answered Sep 23 '22 19:09

Aaron Digulla


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?

like image 34
tim_yates Avatar answered Sep 24 '22 19:09

tim_yates


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

}
like image 43
Pruthvi Minchinadka Avatar answered Sep 21 '22 19:09

Pruthvi Minchinadka


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);
like image 32
Bae Cheol Shin Avatar answered Sep 24 '22 19:09

Bae Cheol Shin