Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Netbeans IDE not issuing warnings about methods called in constructors

Tags:

java

netbeans

Netbeans IDE is good at spotting code that could give you trouble. Why is a warning not issued for

public class Base
{
    Base(...)
    {
        ...;
    }

    public void foo()
    {
        ...;
    }
}

public class Child extends Base
{
    Child(...)
    {
        super(...);
        foo();
    }
}

given that I'm calling the base class function foo() in the child constructor? Of course that's perfectly legitimate as the base object is constructed by the point foo() is called, but a foo() is implicitly a virtual method so really I would expect a warning unless foo() is marked final.

I think that the IDE should issue a warning if you call any base class function from a child class constructor that is not marked as final.

like image 248
Bathsheba Avatar asked Jun 21 '13 09:06

Bathsheba


People also ask

How do I enable code suggestions in NetBeans?

It is easy to enable Java hints in NetBeans. Select "Tools" from the title bar, then select "Options" from the drop-down menu. Choose the "Editor" option and then select the "Hints" tab. At this point, make sure that the "Language" drop-down is set to "Java" (other choices include PHP, JavaScript, and Ruby).

How do I generate Javadoc comments in NetBeans?

Creating Javadoc StubsPlace the cursor above a method or a class that has no Javadoc, type "/** ", and press Enter . The IDE creates a skeletal structure for a Javadoc comment filled with some content. If you have a Javadoc window open, you will see the changes immediately while you are typing.

How do you automatically construct a constructor in NetBeans?

To quickly insert constructors from a super class in NetBeans we only have to place the cursor in our class and go to Source | Insert Code... (or press Alt+Insert in Windows). For example we create a new Exception class for our application extending the java. lang.

Which version of JDK is required for NetBeans 13?

The Apache NetBeans 13 binary releases require JDK 11+, and officially support running on JDK 11 and JDK 17.


1 Answers

It does.
When you open the file in NetBeans, you should see a yellow lightbulb (hint indicator) next to the call to foo. It's a suggestion that you either:

  • Make Child final
  • Make foo final, private, or static

If you want it to be more obvious, go to Tools > Options, or Edit > Preferences, or Netbeans > Preferences (depends on your OS).
Choose Editor
Choose Hints
Set Language to Java
Expand Initialization
Select Problematic call in the constructor
Change Show as to Error
Click OK

Now the line as well as your file will get a red error badge.

Note, none of this will prevent you from ignoring the indicator. The code is still legal Java, so NB will not prevent you from compiling.

like image 54
Devon_C_Miller Avatar answered Oct 22 '22 15:10

Devon_C_Miller