Cannot use this in a static context

在main函数中使用了this后,会提示出错。

原因是在main函数中不能使用this,main函数属于static,main中必须生成一个确定的对象来调用方法,凡是属于static的,this均不能用。

 

我想在main方法中执行 :URL url = this.getClass().getResource("/images/user.jpg"); 报错。

修改后:URL url = Test.class.getClassLoader().getClass().getResource("/images/user.jpg"); 

Test.class.getClassLoader().getClass()得到了一个实例,所以可以调用。

posted @ 2016-03-22 17:34  JonyDogg  阅读(1222)  评论(0编辑  收藏  举报