你能帮我解决下面的代码吗?错误是:"无法在静态上下文中使用它"
public class Sample2 {
/**
* @param args
*/
public static void main(String[] args)
{
Sample2 sam=new Sample2();
//Below code works fine
System.out.println(sam);
//Below code is displaying error
System.out.println(this);
}
}
Run Code Online (Sandbox Code Playgroud)
小智 29
请参阅"this"关键字指的是当前对象,因为哪种方法处于优先级.因为,您不能使用类的实例调用静态方法.这就是为什么"这个"不能在上面的例子中用于静态方法,因为它试图打印当前没有创建的实例.所以,我认为这就是为什么你会得到一个编译时错误.
| 归档时间: |
|
| 查看次数: |
65825 次 |
| 最近记录: |