Global constants in Groovy

前端 未结 4 1119
名媛妹妹
名媛妹妹 2021-01-31 14:17

It is often desired to declare constants at the top of a script that can be referenced anywhere else in the script. In Groovy, it seems that if you declare a constant using fin

4条回答
  •  旧巷少年郎
    2021-01-31 14:26

    In Groovy 1.8+, you can achieve this using the @Field annotation:

    import groovy.transform.Field
    
    @Field final String MY_CONSTANT = 'constant'
    
    def printConstant() { println MY_CONSTANT }
    
    printConstant()
    

提交回复
热议问题