Java bean vs Groovy bean

Java Beans.
   Generally the java bean class have a lot of variables and accessor(setter and getter) methods. The developer needs to write a setter and getter method for those variable as well as the developer explicitly needs to tell that variables and accessor methods are public. See the below example.

//The bean
public class Country {
    public String name;

    public String getName() {
        return name;

    public void setName(String name) { = name;

//The java main class
public class JavaBeanDemo {
    public static void main(String[] args) {
        Country country = new Country();
The output is.

Groovy Bean.
   But in Groovy it's easy because, the Groovy automatically generate accessor methods for all the class variable and by default the methods and variables have public access. See the below example.

Groovy Bean.
class Country {
    String name;

The main class
def country = new Country()
println country.getName()

The will print the message as.


Popular Posts