开发者

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. 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).

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:

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.)

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).

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? 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 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜