Reputation: 693
I have two groovy classes in the same package:
class GTest {
static Void main(args) {
println G.newInstance().var // $> available
}
}
class G {
String var = "available"
}
When I have a similar reference to G from a java class in the same package var
is no longer visible:
public class JTest {
public static void main(String[] args) {
G g = new G();
System.out.println(g.var); // $> The field G.var is not visible
}
}
When I make var
explicitly public in the groovy class, JTest can access it. Is property scope different depending on the type of caller?
Upvotes: 0
Views: 437
Reputation: 46
Groovy generates getters and setters for class properties. When you leave the modifiers off the field definition, it is actually creating the property as a private field and generating the accessor and mutator methods.
When using Groovy, calling 'g.var' actually calls the accessor (i.e. 'g.getVar()'); it is just allowing you to use the property access style.
If your Java class called 'g.getVar()' it will be able to access the data.
See Groovy Beans for a more lengthy explanation.
Upvotes: 3