Problem in using Tips_Create Global Variables on the fly

i can find this block of code under katalon tips/tricks. I need some help in using this.

void addGlobalVariable(String name, def value) {
MetaClass mc = script.evaluate("internal.GlobalVariable").metaClass
String getterName = "get" + name.capitalize()
mc.static."$getterName" = { -> return value }
//mc.static."$name" = value
}

Question:

1. Do we need use this block code as @keyWord? or under any existing class or By creating new Class?

2. when I am using this code. Inside any class. I can see error near void keyword.

_________________

public class ThisMain{
public static void main(String[] args){
void addGlobalVariable(String name, def value) {
MetaClass mc = script.evaluate("internal.GlobalVariable").metaClass
String getterName = "get" + name.capitalize()
mc.static."$getterName" = { -> return value }
//mc.static."$name" = value
}
}
}

________________________

3. When i run this only “addGlobalVariable” block code as Groovy console, then i am getting

"java.lang.reflect.InvocationTargetException"

and also i can see error near keywords “static”, “script” and “evaluate”

Please provide me some clarification. how can use this block of code in katalon

@Sergii Tyshchenko Please help me in clearing above mentioned errors.