Jaco Van Niekerk
Jaco Van Niekerk

Reputation: 4192

Java constructors pattern

I use the pattern quite a lot:

class Blah
  int a;   
  double b;   
  String c;   
  Date d;   

  public Blah(int a, double b, String c, Date d) {
      super(); // possibly   
      this.a = a;   
      this.b = b;
      this.c = c;
      this.d = d;
  }

This is indeed a great deal of boilerplate for something so simple. I was thinking of a generic object factory to do this with introspection, but this feels very evil (special cases, inheritance, and speed issues). Guice could be used and the constructor skipped altogether, but then manual object creation is going to be ugly.

Is this something I will have to live with in Java or is there a way to avoid this boilerplate?

Upvotes: 6

Views: 369

Answers (1)

darijan
darijan

Reputation: 9795

Try using Lombok (http://projectlombok.org/)

You can generate getters, setters and constructors with mere annotations.

Upvotes: 5

Related Questions