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
  • 5,056
  • 9
  • 62
  • 136

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
  • 11,111
  • 10
  • 71
  • 96
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
  • 67,789
  • 12
  • 98
  • 136
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,369
  • 1
  • 15
  • 28