Close

Groovy - Default Visibility of Classes and Methods

[Updated: Feb 23, 2019, Created: Feb 23, 2019]

In Groovy, classes or methods with no visibility modifier are automatically public

class Task {
    void run() {
    }
}

import java.lang.reflect.Modifier

def modifiers = Task.class.modifiers
println Modifier.toString(modifiers)

modifiers = Task.class.getDeclaredMethod("run").modifiers
println Modifier.toString(modifiers)

Output

public
public

Just like Java, top level classes in Groovy can either be 'public' or 'package-private'. They cannot be 'protected' or 'private'.

Since no explicit modifier means 'public' in Groovy, to have package-private visibility we need to use @PackageScope annotation (next tutorial).


Example Project

Dependencies and Technologies Used:

  • Groovy 2.5.6
  • JDK 11
Default Visibility in Groovy Select All Download
  • groovy-default-visibility
    • src
      • Example1DefaultVisiblity.groovy

    See Also