views:

362

answers:

5

When I run static analysis on the following code:

  public ExtractDBScripts(String resBundleName)
    {
        super();
        m_mainBundle = ResourceBundle.getBundle(resBundleName);
    }

I get the following error:

"JAVA 0058 Constructor 'ExtractDBScripts' calls super()".

What is wrong with calling super() from a constructor?

+12  A: 

Probably just that it's completely unnecessary--that is java's default behavior (it will call super for you). You want to use the explicit call to super() if you need to pass a parameter to a non-default constructor.

A static analysis tool will often point out code that does absolutely nothing or is unnecessary to help you reduce clutter. It will also point out a=a; there is nothing wrong with saying a=a; but it's not actually doing anything.

Bill K
That is not true. The constructor does not need to be parameterized in order to call it explicitly, and it certainly won't throw a compiler error for doing so.
amischiefr
But static analysis tools can warn you that it is unnecessary code, which is what is happening.
Bill K
+2  A: 

Absolutely nothing wrong with it - although it is implicitly there as the first line of a constructor if you don't declare it yourself (i.e. there's no need to declare it)

oxbow_lakes
A: 

There is nothing wrong with calling super() within a constructor so long as it is the first line in your constructor, so long as you are actually extending a class that has a non private constructor.

If the class you are extending has only one constructor, and it's private then you can't call super().

amischiefr
+2  A: 

I presume the tool you are using is objecting to that line of code because it is not required - if you remove it the compiler will automatically insert it.

See the "Subclass Constructors" section here.

William
A: 

I would suggest calling super() in your code on purpose, so that it is obvious to other coders what you want to do. It is part of the programming practices where I work and seems to make sense. There may be a way for you to set up the static analysis tool to ignore calls to super() also depending on the software. IntelliJ has annotations for ignoring specific things when it does static analysis.

Jon
How do you stop it from calling super though? If you can't prevent it why clutter the code putting it in?
gommo