我是java菜菜,谁帮我解释下下面这代码为什么this报错:public class T0
{
int i = 0;
public static void main(String[] args)
{
System.out.println(this.i);
}}是this不能引用入口类吗?this只能存在于heap中?
{
int i = 0;
public static void main(String[] args)
{
System.out.println(this.i);
}}是this不能引用入口类吗?this只能存在于heap中?
只能同过实例化类名的引用打点调用方法和属性
t.i;
这样
因为this代表指向自己对象的引用,而静态方法是属于类的,不属于对象,其成功加载后,对象还不一定存在,
即使存在,也不知道this指的是哪一个对象.
什么是入口类?你是不是想说入口函数?
this表示的是当前的对象,而static 属于整个类,所以在static方法中用this行
不同。
不同。OK!!!
public class T0
{
int i = 0;
public static void main(String[] args)
{
System.out.println(T0.i);
} }
public class T0
{
int i = 0;
public static void main(String[] args)
{
TO to=new TO();
System.out.println(to.i);
} }
static就是为了不用创建实例就能访问,开发JAVA的人员费好大劲才想出来de !
多不容易啊!
你偏添个this,历史倒退了呀!
初学者可以理解,其他人不能犯类似的错误
以后大家一起加油~