Alternative to calling a static method via an instance

自古美人都是妖i 提交于 2019-12-02 12:30:29

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");

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!