How to call getClass() from a static method in Java?
刚才在学习Java 使用properties类,遇到这样的错误:

Cannot make a static reference to the non-static method getClass() from the type Object
以前使用过getClass,不晓得怎么用的,后来在stackoverflow看到同样的问题
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:
publicstaticvoid startMusic()
{
URL songPath=getClass().getClassLoader().getResource("background.midi");
}
However Eclipse tells me:
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?
牛人的回答是这样的:
s 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:
classMyClass{
publicstaticvoid 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 =newObject(){}.getClass().getEnclosingClass();
It uses a nested anonymous Object subclass to get hold of the execution context. This trick has a benefit of being copy/paste safe...
How to call getClass() from a static method in Java?的更多相关文章
- Spring MVC exception - Invoking request method resulted in exception : public static native long java.lang.System.currentTimeMillis()
最近在线上系统发现下面的异常信息: 2014-10-11 11:14:09 ERROR [org.springframework.web.servlet.mvc.annotation.Annotati ...
- java.lang.NoSuchMethodError: No static method setLayoutDirection(Landroid/graphics/drawable/Drawable;I)V in class Landroid/support/v4/graphics/drawable/DrawableCompat
Bug: java.lang.NoSuchMethodError: No static method setLayoutDirection(Landroid/graphics/drawable/Dra ...
- java.lang.NoSuchMethodError: No static method getFont
最近在Android Studio升级3.0后,在AlertDialog弹窗时报出了如下问题: java.lang.NoSuchMethodError: No static method getFon ...
- When to use static method in a java class
First , please understand its feature : * no need to instantiate a instance, i.e. simply you can jus ...
- Python Static Method
How to define a static method in Python?Demo: #!/usr/bin/python2.7 #coding:utf-8 # FileName: test.py ...
- Python OOP(2)-static method,class method and instance method
静态方法(Static Method): 一种简单函数,符合以下要求: 1.嵌套在类中. 2.没有self参数. 特点: 1.类调用.实例调用,静态方法都不会接受自动的self参数. 2.会记录所有实 ...
- Hint: Fallback method 'public java.lang.String queryUserByIdFallback(java.lang.Long)' must return: User or its subclass
1.错误日志 熔断器添加错误方法返回时,报了一个 error. com.netflix.hystrix.contrib.javanica.exception.FallbackDefinitionExc ...
- Error during generated code invocation: com.intellij.debugger.engine.evaluation.EvaluateException: Method threw 'java.lang.IllegalAccessError' exception.
场景描述: 再从该数据库中读取数据进行处理的时候,需要将某个字段加入到一个动态的map中,然后需要对该map进行filter过滤,在执行过滤方法的时候报错 Error during generated ...
- Attempt to invoke interface method 'boolean java.util.List.add(java.lang.Object)' on a null
1.Android Studio报错 Attempt to invoke interface method 'boolean java.util.List.add(java.lang.Object)' ...
随机推荐
- tomcat启动异常(严重: Dispatcher initialization failed Unable to load configuration. - [unknown location] )
严重: Dispatcher initialization failed Unable to load configuration. - [unknown location] at com.opens ...
- 【8-23】MFC学习笔记 01
太难了,慢慢学....
- 【8-17】HTML测试
Source : http://www.w3school.com.cn/html HTML 测验 结果:9/20 您的回答: 1.HTML 指的是? 您的回答:超文本标记语言(Hyper Text M ...
- 发布自己的包到Nuget上
1.首先下载Nuget.exe https://dist.nuget.org/index.html 2.设置环境变量 设置apikey nuget setApiKey <my_api_key& ...
- UGUI事件解析
http://www.tuicool.com/articles/7fYjMr http://www.xuanyusong.com/archives/3325
- 2015年11月30日 spring初级知识讲解(一)装配Bean
序,Spring的依赖注入是学习spring的基础.IOC为控制反转,意思是需要的时候就由spring生成一个,而不是先生成再使用. 写在前面 Spring提供面向接口编程,面向接口编程与依赖注入协作 ...
- KNN-实现文本分类
现在大多程序.关于算法的都封装的差不多了... 所以很多程序猿很少来进行深入来研究了... 以前也想过自己好好学习下.但是理论确实难以下咽.怪我喽... 这次项目中需要用到了.要实现对文本进行分类的一 ...
- 你真的懂了R中的stem函数是如何绘制茎叶图的么?
本文原创,转载请注明出处,本人Q1273314690(交流学习) 哭晕 你真的学会了stem()函数了吗? stem()函数的使用方法是: stem(x, scale=1,width=80, at ...
- POJ 2299 Ultra-QuickSort
离散化+树状数组求逆序数 Ultra-QuickSort Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 35024 Accept ...
- JAVA 如何使JScrollPane中的JTextArea自动滚动到最后一行?
1.要使JTextArea带有滚动条,需将JTextArea对象添加到JScrollPane中. JTextArea logArea = new JTextArea(15, 35); //创建JTex ...