简体   繁体   中英

Usage of static and final modifiers in Groovy

class GroovyHello {
    public String execute() {
             println("Test String is " + TEST)
    }

private static final String TEST = "Test"
}

Output for the above snippet in Groovy V.1.6.3 is

Test String is Test

Output for the above snippet in Groovy V.1.8.6 is

Test String is null

The above snippet prints the string successfully if I modify the declaration to have either static ( private static String TEST = "Test" ) or final ( private final String TEST = "Test" ), but not both.

My theory that since object Static and Private then you don't have access to it as it is a separate object. However if it is just private then your method is part of the object and it has access to it. If it is just static then you have access to the field - the field is public by default.

We noticed this happening when we had Groovy++ in the runtime classpath from other transitive dependencies. If that's the case, you might look at that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM