1
votes

Say if I have a class in java and I want it to be a javabean(those getter and setter methods)

The class that I write:

class MyClass{
    String var1;
    Integer var2;
    ........
    many other properties
    ........
}

After partial code generation based on javabean rules:(If there is some tool!)

class MyClass{
    String var1;
    Integer var2;
    ........
    many other properties
    ........

    public void setVar1(String var1){
       this.var1=var1;
    }
    public String getVar1(){
       return var1;
    }

    public void setVar2(Integer var2){
       this.var1=var1;
    }

    public Integer getVar2(){
       return var2;
    }
    ............
    Many other getter and setter method
    ............
}

It is very tedious to have to write all those getter and setter methods for all the properties in order to make MyClass a javabean.

Is there any software or IDE plugin that can help do the rest of the work as long as I just give the properties. Even though some of the assumingly-generated code is not correct, but modifying just some of the generated code is much faster and delightful than having to write those codes on my own.

2

2 Answers

2
votes

Any IDE worth the name will do this for you. For example, in Eclipse it's right-click > Source -> Getters and Setters.

Different in other IDEs, obviously, but they do the same thing.

1
votes

Eclipse & Netbeans and perhaps other IDE's have a "generate getters and setters"-shortcut.