The moose likes Groovy and the fly likes Groovy Beans Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Other Languages » Groovy
Reply Bookmark "Groovy Beans" Watch "Groovy Beans" New topic
Author

Groovy Beans

Rob Wednesday
Greenhorn

Joined: Feb 02, 2005
Posts: 11
Am I missing something?

The concept of automagic properties is nice, but not that useful because generated methods have same permission as fields. The point of a bean/properties is to provide encapsulation. Private fields do not generate access methods. Public fields should typically be avoided.

Doc at: http://docs.codehaus.org/display/GROOVY/Groovy+Beans


class One {
private int xyz
int abc
}

class Two {
private int xyz

int getXyz() {return xyz} // not created automatically for private
void setXyz(int x) {xyz=x} // not created automatically for private
}

def o=new One()
//o.setXyz(1111)// causes exception
o.setAbc(123)// works

def t=new Two()
t.setXyz(222)


Rob.
Tug Wilson
Ranch Hand

Joined: Dec 12, 2006
Posts: 33
I think you may be misreading the example.

Groovy properties are declared vert much like fields but they result in private fields and public getters and setters.
Tug Wilson
Ranch Hand

Joined: Dec 12, 2006
Posts: 33
I have made minor changes to the page and have corrected a couple of errors.

I hope you find it clearer now
Rob Wednesday
Greenhorn

Joined: Feb 02, 2005
Posts: 11
Thanks for your help.

I got off track in an initial test because o.xyz worked. I forgot that it was accessible because of package permissions.

Rob.
 
 
subject: Groovy Beans
 
developer file tools