2

I have a class U and in the constuctor of the class I call overridable method which is public. NetBeans warns me: Overridable method call in constructor

However, I don't override the method in my project, since class U has no subclasses.. Is it OK to leave it like this? ....calling overridable method in the constructor in this case?

JAN ORTS
  • 63
  • 1
  • 4
  • Possible duplicates: [What's wrong with overridable method calls in constructors?](http://stackoverflow.com/questions/3404301/whats-wrong-with-overridable-method-calls-in-constructors), [How to fix “Constructor Calls Overridable Method”](http://stackoverflow.com/questions/10337011/how-to-fix-constructor-calls-overridable-method). See also: [Constructors shouldn't call overridables](http://www.javapractices.com/topic/TopicAction.do?Id=215). – ZeroOne Mar 13 '13 at 09:20
  • check this one http://stackoverflow.com/questions/3404301/whats-wrong-with-overridable-method-calls-in-constructors – pomkine Mar 13 '13 at 09:21

3 Answers3

2

It is not an error. You can ignore it.

If you want to make the compiler happy, make either method, or the whole class final.

Valeri Atamaniouk
  • 5,125
  • 2
  • 16
  • 18
2

As already mentioned you can 'ignore' the warning.

However you do so at your own risk as bugs can creep in at a later date. The reason for the warning is that the compiler cannot prove that a reference to 'this' will not escape the constructor. Which can lead to bugs, as the object being created has not been fully constructed yet and thus the object can be in an invalid state.

Chris K
  • 11,622
  • 1
  • 36
  • 49
1

It is a warning, not an error, so you can just leave it like this. If you would publish this code however, someone could extend your class U, override the method and get into a lot of trouble.

Vincent van der Weele
  • 12,927
  • 1
  • 33
  • 61