Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 885
dhanu05 Avatar asked Nov 04 '22 03:11

dhanu05


1 Answers

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.

like image 179
MeIr Avatar answered Nov 15 '22 06:11

MeIr