Groovy - Default Visibility of Classes and Methods

[Last Updated: Aug 11, 2020]

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)



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 9.0.1
Default Visibility in Groovy Select All Download
  • groovy-default-visibility
    • src
      • Example1DefaultVisiblity.groovy

    See Also