简体   繁体   English

访问Java类的静态字段时出现问题

[英]Problem accessing a static field of a Java class

I am having an inexplicably hard time doing something that I thought was simplicity itself. 我在做一些我认为简单的事情时遇到了一件莫名其妙的事情。 I have a JAR file on my classpath. 我的classpath上有一个JAR文件。 I'm in Emacs, using a SLIME REPL, and I'm trying to access a static field of an instance of a Java class (one inside the JAR). 我在Emacs中,使用SLIME REPL,我正在尝试访问Java类实例的静态字段(JAR中的一个)。

Here's my class: 这是我的班级:

public class MainFrame extends JFrame implements WindowListener,
        TreeSelectionListener {
    JPanel panel;
    InfocardWindow infoWindow;
    InfocardBuilder infocardBuilder;
    Main infomlFile;
    static NotecardModel setupModel;
    ...

When I tried: 当我尝试:

infwb.cardmaker> (import 'javax.swing.JFrame)
javax.swing.JFrame
infwb.cardmaker> (import 'org.infoml.infocardOrganizer.MainFrame)
org.infoml.infocardOrganizer.MainFrame
infwb.cardmaker> MainFrame/setupModel
; Evaluation aborted.

The error message was: 错误消息是:

Unable to find static field: setupModel in class org.infoml.infocardOrganizer.MainFrame
  [Thrown class java.lang.Exception]

I tried switching to a simpler problem: accessing a non-static field. 我尝试切换到一个更简单的问题:访问非静态字段。 I did it inside a let , to eliminate the possibility that doing this from the REPL might be the source of the problem: 我在let做了它,以消除从REPL执行此操作可能是问题的根源的可能性:

infwb.cardmaker> (let [mainFr (MainFrame.)]
  (println (.panel mainFr)))
; Evaluation aborted.

The error message was: 错误消息是:

No matching field found: panel for class org.infoml.infocardOrganizer.MainFrame
  [Thrown class java.lang.IllegalArgumentException]

I got the same result when substituting (.panel mainFr) and (println (. mainFr panel) in the body of the let . Also, no change when switching the REPL to namespace user . (Granted, these are shake-a-dead-chicken voodoo desperation moves.) let的主体中替换(.panel mainFr)(println (. mainFr panel)时,我得到了相同的结果。另外,在将REPL切换到命名空间user时没有变化。(当然,这些都是震动 - 死了 -鸡巫毒绝望的动作。)

Google queries like 'emacs slime clojure unable to access Java class field error "Unable to find static field"' yield nothing useful--most have to do with trying to call Java class methods (not access Java class fields). 谷歌查询像'emacs slime clojure无法访问Java类字段错误“无法找到静态字段”'没有任何用处 - 大多数都与尝试调用Java类方法(不访问Java类字段)有关。

Just to be thorough, I tried: 为了彻底,我试过:

user> (let [mainFr (MainFrame.)]
  MainFrame/setupModel)
; Evaluation aborted.

The error message was, as before: 错误消息和以前一样:

Unable to find static field: setupModel in class org.infoml.infocardOrganizer.MainFrame
  [Thrown class java.lang.Exception]

Bottom line: Given an instance of MainFrame, what do I need to do to access either a static or non-static field? 底线:给定一个MainFrame实例,我需要做什么才能访问静态或非静态字段? Thanks for any help or hints you can provide. 感谢您提供的任何帮助或提示。

Read the Controlling Access to Members of a Class tutorial. 阅读控制对类成员的访问教程。 You'll find that you need to either use the public modifier, or be aware that since there is no modifier (the default, also known as package-private), it is visible only within its own package. 你会发现你需要使用public修饰符,或者要注意,因为没有修饰符(默认值,也称为package-private),它只在自己的包中可见。

public class MainFrame extends JFrame implements WindowListener,
50                  TreeSelectionListener {
51          JPanel panel;
52          InfocardWindow infoWindow;
53          InfocardBuilder infocardBuilder;
54          Main infomlFile;
55          static NotecardModel setupModel;
            ...
}

The field is not publicly accessible. 该字段无法公开访问。 Read the source . 阅读来源 You need to use the public modifier. 您需要使用public修饰符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM