刚才在学习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?的更多相关文章

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

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

  3. java.lang.NoSuchMethodError: No static method getFont

    最近在Android Studio升级3.0后,在AlertDialog弹窗时报出了如下问题: java.lang.NoSuchMethodError: No static method getFon ...

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

  5. Python Static Method

    How to define a static method in Python?Demo: #!/usr/bin/python2.7 #coding:utf-8 # FileName: test.py ...

  6. Python OOP(2)-static method,class method and instance method

    静态方法(Static Method): 一种简单函数,符合以下要求: 1.嵌套在类中. 2.没有self参数. 特点: 1.类调用.实例调用,静态方法都不会接受自动的self参数. 2.会记录所有实 ...

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

  8. Error during generated code invocation: com.intellij.debugger.engine.evaluation.EvaluateException: Method threw 'java.lang.IllegalAccessError' exception.

    场景描述: 再从该数据库中读取数据进行处理的时候,需要将某个字段加入到一个动态的map中,然后需要对该map进行filter过滤,在执行过滤方法的时候报错 Error during generated ...

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

随机推荐

  1. SQL having 子句

    1.为什么存在? 在 SQL 中增加 HAVING 子句原因是,WHERE 关键字无法与合计函数一起使用. 2.举例子: SELECT Customer,SUM(OrderPrice) FROM Or ...

  2. JavaScript 学习笔记 -- Function

    JS 中 函数.继承.闭包.作用域链... 一直都是硬伤,一碰到这样的问题头就大了.但是如果我继续着说:我不会,就真的无药可救了.要勇敢地说出:我的字典里就没有不会这个词,吼吼..正好昨天在书城里看了 ...

  3. Oracle CASE WHEN 用法介绍

    1. CASE WHEN 表达式有两种形式 --简单Case函数 CASE sex WHEN '1' THEN '男' WHEN '2' THEN '女' ELSE '其他' END --Case搜索 ...

  4. jQuery回调、递延对象总结(上篇)—— jQuery.Callbacks

    前言: 作为参数传递给另一个函数执行的函数我们称为回调函数,那么该回调又是否是异步的呢,何谓异步,如:作为事件处理器,或作为参数传递给 (setTimeout,setInterval)这样的异步函数, ...

  5. css3动画由浅入深总结

    阅读目录 一:过渡动画---Transitions 二:Animations功能 三:translate(tx,ty) 四:scale(x,y) 五:rotate(x): 5.1:skew(x,y): ...

  6. [译]Profile and debug your ASP.NET MVC app with Glimpse

    原文:http://www.asp.net/mvc/overview/performance/profile-and-debug-your-aspnet-mvc-app-with-glimpse Gl ...

  7. 新年新技术:MongoDB 3.0

    前一篇介绍了HTTP/2,这一篇简单介绍下3月3号发布的MongoDB 3.0. What’s new in MongoDB 3.0? 新的存储引擎WiredTiger MongoDB 3.0的存储引 ...

  8. java之远程接口调用

    一.通过地址栏传值 1.项目结构 2.pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi ...

  9. UNIX Time 时间戳 与 北京时间 相互转换

    typedef struct t_xtime { int year; int month; int day; int hour; int minute; int second; } _xtime ; ...

  10. iOS开发——UI基础-按钮的创建和设置

    @interface ViewController () - (IBAction)customBtnClick; @end @implementation ViewController - (void ...