0

I am initializing some member variables in my DTO via setters from inside the constructor.

But the below pmd error showing so how to eliminate that pmd rule violation?

Overridden method 'setAbc' called during object construction

class A{

private String x;

public getX(){
return x;
}
public setX(String x){
this.x = x ;
}
A(){}

A(B b){
setX("C");
}

}

Harshana
  • 7,297
  • 25
  • 99
  • 173
  • 3
    What's the most important is to understand why PMD shows this error. Once understood, you will also understand how to avoid it, and avoid making the mistake again. – JB Nizet Oct 13 '11 at 12:23
  • I recommend you to read http://stackoverflow.com/questions/3404301/whats-wrong-with-overridable-method-calls-in-constructors – user1154664 Aug 17 '12 at 01:38

1 Answers1

5

How about making method setX final? Or perhaps event the entire class?

Ben van Gompel
  • 747
  • 4
  • 12