0

For example I have the following object:

public class Dog{
    private String name;
    private int age;
    private int legs;
    private Color color;

    /*getters and setters*/
}

And I want to initialize it and set all properties not by constructor but by using setters:

Dog dog = new Dog();
dog.setName("Rex");;
dog.setAge(4);
...

Can I generate code which set all fields from above in the easy way?

Anatoly
  • 4,888
  • 7
  • 50
  • 123

3 Answers3

1

It's cumbersome, but what I do:

Use Eclipse's Source -> Generate Getters and Setters... function (also ALT + SHIFT + S) and then just replace all '=' characters with '(' and ';' with ');'. finally I go through every line and press ctrl-space to let Eclipse finish the method call with proper casing (configure Eclipse to overwrite instead of insert code assist suggestions).

That or do a regex replacement if it is a lot.

Benny Bottema
  • 10,305
  • 10
  • 66
  • 89
0

You can introduce a setAllValues method, that should take all the attributes of your class as parameters. And simply call the setters inside that method.

Juned Ahsan
  • 66,028
  • 11
  • 91
  • 129
0

You can create a new template in eclipse in Preferences/Java/Editor/Templates and then just use it (similar to 'syso' ctrl+space that produces System.out.println)

Nadir
  • 1,331
  • 1
  • 15
  • 28