GroovyBeans are JavaBeans but using a much simpler syntax. Here's an example class Customer { // properties Integer id String name Date dob // sample code static void main(args) { customer = new Customer() customer.id = 1 customer.name = "Gromit" println("Hello ${customer.name}") } } Hello Gromit Notice how the properties look just like public fields. In Groovy fields and properties have been merged so that they act and look the same. So the above is equivlent to the following Java code... public class Customer { // properties private Integer id private String name private Date dob public Integer getId() { return id; } public Integer getId() { return id; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public void setName(String name) { this.name = name; } public void setDob(Date dob) { this.dob = dob; } // sample code public static void main(String[] args) { Customer customer = new Customer(); customer.setId(1); customer.setName("Gromit"); println("Hello " + customer.getName()); } } When Groovy is compiled to bytecode the following rules are used.
So for example you could create a public read only property with a protected setter or a read only property like this class Foo { // read only property private String name public String getName() { return name } // read only property with protected setter Integer amount protected void setAmount(Integer amount) { this.amount = amount } // dynamically typed property cheese } |