Alternative to calling a static method via an inst

2019-03-03 11:12发布

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 apparently a "pro" would not do it thus as showMessageDialog is static. Is there a better way to write this?

2条回答
你好瞎i
2楼-- · 2019-03-03 11:25

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.

查看更多
一夜七次
3楼-- · 2019-03-03 11:42

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");
查看更多
登录 后发表回答