BeastMaster64
BeastMaster64

Reputation: 167

How to access global variables in groovy

I have a really simple groovy script-

import groovy.transform.Field

@Field final static String VARIABLE = 'Variable'

static void main(String[] args) {
    println VARIABLE
}

But the output is empty, how can I make a global, static constant in groovy and use it in static methods?

Upvotes: 3

Views: 1362

Answers (1)

daggett
daggett

Reputation: 28564

to use a global static constant i would use this syntax - maybe not perfect but always working:

class Const{
  final static String VARIABLE='value'
}

println "VARIABLE=${Const.VARIABLE}"


avoid using static void main() in groovy scripts - this way you could conflict with groovy script-to-class transformation

https://groovy-lang.org/structure.html#_public_static_void_main_vs_script

Upvotes: 2

Related Questions