Groovy - Method Scope and Visibility
Method Scopes and Visibility helps in controlling access to a class's method from other classes or external code. Groovy being JVM based, follows the scope and visibility concepts of Java with few nuances.
Visibility Modifiers
public − In groovy, a method is by default public which means a method is accessible from other classes or scripts by default.
private − Private access, means a method is accessible within the class in which it is defined. A private method is not accessible by other classes or even subclasses.
protected − A protected method is accessible within the declaring class as well its subclasses. Other classes in same package can also access such methods.
package-private −If a method is not having any modifier, then it is treated as package-private which means method is accessible within the same package. In groovy, if a method is not having modifier then it is treated as public. We can achieve package-private scope using @groovy.transform.PackageScope annotation.
Example - Method Scopes and Visibility
Example.groovy
package com.tutorialspoint
class TestClass {
public void publicMethod() {
println "public method called."
privateMethod()
}
private void privateMethod() {
println "private method called"
}
protected void protectedMethod() {
println "protected method called"
}
void defaultMethod() { // Implicitly public
println "default(Public) method called"
}
}
class TestAnotherClass {
def testInstance = new com.tutorialspoint.TestClass()
void accessMethods() {
testInstance.publicMethod()
// being private, method cannot be accessed
// testInstance.privateMethod()
// protected method is accessible as TestAnotherClass is in same package
testInstance.protectedMethod()
testInstance.defaultMethod()
}
}
def testInstance = new com.tutorialspoint.TestClass()
testInstance.publicMethod()
testInstance.defaultMethod()
// Not accessible
// testInstance.privateMethod()
// Not accessible being outside the package/subclass
// testInstance.protectedMethod()
def testAnotherInstance = new com.tutorialspoint.TestAnotherClass()
testAnotherInstance.accessMethods()
Output
When we run the above program, we will get the following result.
public method called. private method called default(Public) method called public method called. private method called protected method called default(Public) method called
Scopes within a method
Variables declared and used within a method have local scope which means variables are accessible within method only. Once a method finishes execution, variable scope also vanishes.
main.groovy
class Example {
void testScope() {
def localVariable = "I'm local variable"
println localVariable
}
void anotherMethod() {
// it will cause error as localVariable is not available
println localVariable
}
}
def exampleInstance = new Example()
exampleInstance.anotherMethod()
Output
When we run the above program, we will get the following result.
Caught: groovy.lang.MissingPropertyException: No such property: localVariable for class: Example groovy.lang.MissingPropertyException: No such property: localVariable for class: Example at Example.anotherMethod(main.groovy:9)