访问 Java 类的静态字段时出现问题
我在做一些我认为很简单的事情时遇到了莫名其妙的困难。我的类路径中有一个 JAR 文件。我在 Emacs 中使用 SLIME REPL,并且尝试访问 Java 类实例(JAR 内的一个)的静态字段。
这是我的课程:
public class MainFrame extends JFrame implements WindowListener,
TreeSelectionListener {
JPanel panel;
InfocardWindow infoWindow;
InfocardBuilder infocardBuilder;
Main infomlFile;
static NotecardModel setupModel;
...
当我尝试时:
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.
错误消息是:
Unable to find static field: setupModel in class org.infoml.infocardOrganizer.MainFrame
[Thrown class java.lang.Exception]
我尝试切换到一个更简单的问题:访问非静态字段。我在 let
中完成了此操作,以消除从 REPL 执行此操作可能是问题根源的可能性:
infwb.cardmaker> (let [mainFr (MainFrame.)]
(println (.panel mainFr)))
; Evaluation aborted.
错误消息是:
No matching field found: panel for class org.infoml.infocardOrganizer.MainFrame
[Thrown class java.lang.IllegalArgumentException]
替换 (.panel 时,我得到了相同的结果mainFr)
和 (println (.mainFr panel)
在 let
的主体中。此外,将 REPL 切换到命名空间 user< 时也没有任何变化/代码>。(当然,这些是 像“emacs slime clojure 无法访问 Java 类字段错误“无法找到静态字段”这样
的 Google 查询不会产生任何有用的结果——大多数与尝试调用 Java 类方法有关(不访问 Java 类字段)。
为了彻底,我尝试了:
user> (let [mainFr (MainFrame.)]
MainFrame/setupModel)
; Evaluation aborted.
错误消息与以前一样:
Unable to find static field: setupModel in class org.infoml.infocardOrganizer.MainFrame
[Thrown class java.lang.Exception]
底线:给定 MainFrame 的实例,我需要做什么才能访问静态或非静态字段?谢谢获取您可以提供的任何帮助或提示。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
阅读控制对类成员的访问教程。您会发现您需要使用
public
修饰符,或者要注意,由于没有修饰符(默认值,也称为 package-private),因此它仅在其自己的包中可见。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 修饰符。
The field is not publicly accessible. Read the source. You need to use the public modifier.