接口和类中声明的常量之间的区别?

时间:2012-08-08 20:28:52

标签: java oop interface

我遇到了一个我很好奇的特殊问题(想找到解决方法)。

如果我在班上声明变量NODES:

private static final String NODES = "nodes";

一切正常。

但是,如果我在接口中声明变量:

public interface MSMConstants {
    public static final String NODES = "nodes";
}

并在使用变量NODES的类中实现该接口, 我第一次使用NODES时得到一个空指针异常。

关于这里发生了什么的任何想法?

崩溃的行:

lr_fontSize.setSelectedItem(((VisualItem)m_vis.getVisualGroup(NODES).tuples().next()).getFont().getSize());

SearchQueryBinding sq = new SearchQueryBinding((Table) m_vis.getGroup(NODES), "label",
        (SearchTupleSet) m_vis.getGroup(Visualization.SEARCH_ITEMS));

第二个是在构造函数中;第一个也在构造函数中,但对于从第二个实例化的对象。

要求堆栈跟踪:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at prefuse.data.query.SearchQueryBinding.<init>(SearchQueryBinding.java:53)
    at edu.stanford.folding.msmexplorer.MSMExplorer.<init>(MSMExplorer.java:703)
    at edu.stanford.folding.msmexplorer.MSMExplorer.graphView(MSMExplorer.java:1538)
    at edu.stanford.folding.msmexplorer.MSMExplorer.graphView(MSMExplorer.java:1526)
    at edu.stanford.folding.msmexplorer.MSMExplorer$1.actionPerformed(MSMExplorer.java:202)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2028)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2351)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
    at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:272)
    at java.awt.Component.processMouseEvent(Component.java:6375)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
    at java.awt.Component.processEvent(Component.java:6140)
    at java.awt.Container.processEvent(Container.java:2083)
    at java.awt.Component.dispatchEventImpl(Component.java:4737)
    at java.awt.Container.dispatchEventImpl(Container.java:2141)
    at java.awt.Component.dispatchEvent(Component.java:4565)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4619)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4280)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4210)
    at java.awt.Container.dispatchEventImpl(Container.java:2127)
    at java.awt.Window.dispatchEventImpl(Window.java:2482)
    at java.awt.Component.dispatchEvent(Component.java:4565)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:684)
    at java.awt.EventQueue.access$000(EventQueue.java:85)
    at java.awt.EventQueue$1.run(EventQueue.java:643)
    at java.awt.EventQueue$1.run(EventQueue.java:641)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
    at java.awt.EventQueue$2.run(EventQueue.java:657)
    at java.awt.EventQueue$2.run(EventQueue.java:655)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:654)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

2 个答案:

答案 0 :(得分:2)

没有技术差异。我们可以在接口和类上声明final static常量并使用它们。您展示的两种方法都应该有效。

但是,常量声明与接口的概念不太匹配。我知道,很多人使用接口作为“常量存储库”,这绝对不是他们的目的。

这样做的一个原因是它提供了一种通过“实现”那些常量持有者接口来“继承”常量定义的简单方法。

但现在我们有静态导入,虽然不再需要“滥用”接口。

答案 1 :(得分:0)

好吧,我不确定它为什么会产生NullPointerException,但是如果你有两个同名的静态变量,你应该在它们前面加上类名或接口名,这样Java知道你的意思是哪个静态变量。

您是否可以更改代码以使用MSMConstants.NODESMyClassName.NODES(例如)并查看是否可以解决问题?