Global constants in Groovy

前端 未结 4 1115
名媛妹妹
名媛妹妹 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:36

    The another efficient way to add the global application level constants are declare one interface in suitable package as

    interface applicationConstants {
    //All constants goes here.
        static final float PI = 3.14 
        String ADMIN_USER = "ADMIN"
        Map languages = [
            "en":   "English",
            "hi":   "Hindi",
            "mr":   "Marathi"
    
        ]
    // Like above you can declare all application level code constants here.
    
    }
    

    Use of constants in any class as below,

     import packageNameContainingInterface.applicationConstants // import statement.
     def adminUser = applicationConstants.ADMIN_USER
     println adminUser
    

提交回复
热议问题