码迷,mamicode.com
首页 > 其他好文 > 详细

Cannot use this in a static context

时间:2016-03-22 18:49:58      阅读:362      评论:0      收藏:0      [点我收藏+]

标签:

在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()得到了一个实例,所以可以调用。

Cannot use this in a static context

标签:

原文地址:http://www.cnblogs.com/dudefu/p/5307735.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!