Warning Question

Hello, JGO community.

When I write (Java) applications, I really don’t like having any warnings.
Right now, I am working on a simple Breakout game and I decided to use
Netbeans. Basically, I have a code that in Netbeans shows a warning,
while in Eclipse it doesn’t. I am wondering why. Here is the code:


package game;

import java.awt.Color;
import javax.swing.JPanel;

public class Board extends JPanel{
    public Board(String name){
        init(name);
    }
  
    public void init(String name){
        setName(name);
        setBackground(Color.WHITE);
        setFocusable(true);
    }
}


In NetBeans, there is the warning on this line:

init(name); // WARNING: Overridable method call in constructor.

I understand that this is highly irrelevant, but I just want to know how
can I fix that warning without suppressing the warnings for the constructor.

Well. it’s just warning you that you are using an overridable method in the constructor, which I suppose means if someone extends your class, they can change the behavior of the constructor if they override the init-method.
I’m not really sure that you can do much about it, but I’d recommend making the init()-method private, as it should only be used by that class to initialize its variables.

Also, why do you pass the name on to init? Why not just set the name in the constructor? It’s not likely that you want your program to change its name in the middle of execution anyway. The other calls are sitting fine in the init, though.

The thing with overrideable method calls in constructors is that the overridden method can access stuff in the superclass that has not yet been initialised which can make for some proper hair-pulling moments.

Cas :slight_smile:

To fix it do any of:
make the class final
make the method private
make the method final

Program’s name is stored ‘n’ set in other class named Frame. That was JPanel’s name. For some reason I want to set the name of JPanel. :stuck_out_tongue:

Thank you all for showing the interest to help me.
I will most likely break my habbit and leave the warning
there, but I’m not sure yet. Once again, thanks.

I wonder if that applies for protected functions, too. Most warnings in Eclipse and NetBeans are generated by the IDE, so you won’t necessarily get the same warnings in all developer environments. You can turn individual types of warnings on or off or instruct your IDE to treat certain types of warnings as errors. I would make the function private if you do anything to address the error. (Making it implicitly final and invisible to subclasses.)

You can also do


package game;

import java.awt.Color;
import javax.swing.JPanel;

public class Board extends JPanel{
    public Board(String name){
        init(name);
    }
  
-   public void init(String name){
+   private void init(String name){
        setName(name);
        setBackground(Color.WHITE);
        setFocusable(true);
    }

+   public void reinitialize(String name)
+   {
+      init(name);
+   }
}