Alternative to calling a static method via an instance

后端 未结 2 1156
半阙折子戏
半阙折子戏 2021-01-27 17:45
JOptionPane jop = new JOptionPane( );
jop.showMessageDialog(“This is never done”);

I\'m told that this is done in poor taste. I mean it works, but app

相关标签:
2条回答
  • 2021-01-27 18:06

    Why are you creating an object to simply call its static method? There's no reason for it. JOptionPane.showMessageDialog("This is never done"); is all you need.

    0 讨论(0)
  • 2021-01-27 18:16

    A static method can be invoked without a reference to an instance:

    JOptionPane.showMessageDialog("This is never done");
    

    Actually, these line:

    JOptionPane jop = new JOptionPane();
    jop.showMessageDialog("This is never done");
    

    will be converted at compile time to:

    JOptionPane jop = new JOptionPane();
    JOptionPane.showMessageDialog("This is never done");
    
    0 讨论(0)
提交回复
热议问题