本文翻译自:How to call getClass() from a static method in Java?
I have a class that must have some static methods. 我有一个必须具有一些静态方法的类。 Inside these static methods I need to call the method getClass() to make the following call: 在这些静态方法中,我需要调用方法getClass()进行以下调用:
public static void startMusic() {
URL songPath = getClass().getClassLoader().getResource("background.midi");
}
However Eclipse tells me: 但是Eclipse告诉我:
Cannot make a static reference to the non-static method getClass()
from the type Object
What is the appropriate way to fix this compile time error? 解决此编译时错误的适当方法是什么?
#1楼
参考:https://stackoom.com/question/Yipn/如何从Java中的静态方法调用getClass
#2楼
As for the code example in the question, the standard solution is to reference the class explicitly by its name, and it is even possible to do without getClassLoader()
call: 对于问题中的代码示例,标准解决方案是通过其名称显式引用该类,甚至可以在没有getClassLoader()
调用的情况下进行操作:
class MyClass {
public static void startMusic() {
URL songPath = MyClass.class.getResource("background.midi");
}
}
This approach still has a back side that it is not very safe against copy/paste errors in case you need to replicate this code to a number of similar classes. 这种方法还有一个缺点,那就是如果您需要将此代码复制到许多类似的类中,则对于复制/粘贴错误并不是十分安全。
And as for the exact question in the headline, there is a trick posted in the adjacent thread : 至于标题中的确切问题,在相邻线程中有一个技巧:
Class currentClass = new Object() { }.getClass().getEnclosingClass();
It uses a nested anonymous Object
subclass to get hold of the execution context. 它使用嵌套的匿名Object
子类来获取执行上下文。 This trick has a benefit of being copy/paste safe... 此技巧的好处是可以安全复制/粘贴...
Caution when using this in a Base Class that other classes inherit from: 在其他类继承的基类中使用该类时的警告:
It is also worth noting that if this snippet is shaped as a static method of some base class then currentClass
value will always be a reference to that base class rather than to any subclass that may be using that method. 还值得注意的是,如果此代码段的形状是某些基类的静态方法,则currentClass
值将始终是对该基类的引用,而不是对可能使用该方法的任何子类的引用。
#3楼
I wrestled with this myself. 我自己为此搏斗。 A nice trick is to use use the current thread to get a ClassLoader when in a static context. 一个不错的技巧是在静态上下文中使用当前线程获取ClassLoader。 This will work in a Hadoop MapReduce as well. 这也将在Hadoop MapReduce中工作。 Other methods work when running locally, but return a null InputStream when used in a MapReduce. 其他方法在本地运行时可以工作,但在MapReduce中使用时返回null InputStream。
public static InputStream getResource(String resource) throws Exception {
ClassLoader cl = Thread.currentThread().getContextClassLoader();
InputStream is = cl.getResourceAsStream(resource);
return is;
}
#4楼
Try something like this. 尝试这样的事情。 It works for me. 这个对我有用。 Logg (Class name) Logg(类名)
String level= "";
Properties prop = new Properties();
InputStream in =
Logg.class.getResourceAsStream("resources\\config");
if (in != null) {
prop.load(in);
} else {
throw new FileNotFoundException("property file '" + in + "' not found in the classpath");
}
level = prop.getProperty("Level");
#5楼
在Java7 +中,您可以在静态方法/字段中执行此操作:
MethodHandles.lookup().lookupClass()
#6楼
Try it 试试吧
Thread.currentThread().getStackTrace()[1].getClassName()
Or 要么
Thread.currentThread().getStackTrace()[2].getClassName()