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

前端 未结 8 977
我寻月下人不归
我寻月下人不归 2020-11-28 22:29

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条回答
  •  天涯浪人
    2020-11-28 22:36

    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
    
    }
    

提交回复
热议问题