2
votes

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?

3

3 Answers

2
votes

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.

1
votes

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.

1
votes

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.