When specifying types, javac “uses unchecked or unsafe operations”

The following codes:

public void addGrillaListener(Stage stageToClose,Grilla listener)
{
    GrillaHandler<WindowEvent> handy = new GrillaHandler<>(listener);
    if(stageToClose!=null)
    {
        stageToClose.addEventHandler(WindowEvent.WINDOW_HIDDEN,handy);
    }
}

Causes the compiler to display the message How can I avoid it?

Additional information:

>Grilla is a stage interface > grillahandler is a subclass of EventHandler, which takes Grilla as a constructor parameter > use JDK 7, grillahandler < > allowed > compiler message is non-specific - it declares that this method uses unchecked or unsafe operations > stage is a class provided by Oracle, which is a part of JavaFX

GrillaHandler:

public class GrillaHandler<T> implements EventHandler {

    private Grilla win;

    public GrillaHandler(Grilla win) {
        this.win=win;
    }

    @Override
    public void handle(Event t) {
        win.loadTable();
    }
}

Greera:

public interface Grilla { 
    public void loadTable();
}

Solution

Change code to

public class GrillaHandler<T extends Event> implements EventHandler<T>{ 
//...
}

JavaFX EventHandler is a paremeterized type You are missing that in the declaration of the grillahandler You are forced to provide type parameters or redeclare type parameters in class declarations because you seem to need to do so according to your declarations

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>