this和static使用疑惑,谢谢!

xuancao 2003-06-30 09:29:29
this代表当前对象的应用
static代表类成员
程序一运行正常,可是程序二运行错误,请帮忙.
public class staticTest{
public String name="dalily";
public static void main(String args[]){
staticTest a = new staticTest();
System.out.print(a.name);
}
}
public class staticTest{
public String name="dalily";
public static void main(String args[]){
//staticTest a = new staticTest();
System.out.print(this.name);
}
}
...全文
26 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
xuancao 2003-07-01
  • 打赏
  • 举报
回复
谢谢,现在总算是理解了!
helpall 2003-07-01
  • 打赏
  • 举报
回复
这样想一下:
static是公共的工具,其行为是一样的,每个人都能用公共的工具;
没有static修饰的方法在每个实例中的行为可能是不一样的,公共的工具就不能用它,除非是指定哪一个实例的方法.
javabandit 2003-06-30
  • 打赏
  • 举报
回复
同意楼上的,而且上面的很多解释都不错,值得学习!
stonegump 2003-06-30
  • 打赏
  • 举报
回复
看一下Thinking in java,讲的很好
SailorK 2003-06-30
  • 打赏
  • 举报
回复
public class statictest{
public static String name="dalily";
public static void main(String args[]){
statictest a = new statictest();
System.out.print(statictest.name);
}
}

这个程序编译通过能运行,因为public static String name="dalily";中把name设成了static,符合yuanmeng163(今天我有空) 的解说
SailorK 2003-06-30
  • 打赏
  • 举报
回复
谢谢楼上的yuanmeng163(今天我有空) ,我看懂了,长知识了!!!
XKP 2003-06-30
  • 打赏
  • 举报
回复
而a是main方法内的局部变量,引用是没问题的!


··············
good
yuanmeng163 2003-06-30
  • 打赏
  • 举报
回复
呵~~~
大伙在这讲了半天,,,,你还没明白啊。:(

这样说吧,Java语法规则有一条是:静态方法不能引用外部非静态成员变量(注意是外部)。
this是代表这个类的一个实例,就相当于:
public class staticTest{
staticTest this = new staticTest();//这个类创建时就会自动执行这一句(当然实际并不能这样写)
public String name="dalily";
public static void main(String args[]){
staticTest a = new staticTest();
System.out.print(a.name);
}
}
这个this是这个类的成员变量,并不是static的,所以在static的main方法中并不能引用!
而a是main方法内的局部变量,引用是没问题的!
xuancao 2003-06-30
  • 打赏
  • 举报
回复
staticTest a = new staticTest()中a也是对象的引用
a和this有什么区别
这儿根本就不是所谓的"静态方法不能调用非静态成员变量",而是都是通过对象成员来访问给成员变量.
xuancao 2003-06-30
  • 打赏
  • 举报
回复
staticTest a = new staticTest()中a也是对象的引用
a和this有什么区别
这儿根本就不是所谓的"静态方法不能调用非静态成员变量",而是都是通过对象成员来访问给成员变量.
yuanmeng163 2003-06-30
  • 打赏
  • 举报
回复
public class staticTest{
public String name="dalily";
public static void main(String args[]){
staticTest a = new staticTest();
System.out.print(a.name);
}
}


public class staticTest{
public static String name="dalily";
public static void main(String args[]){
staticTest a = new staticTest();
System.out.print(staticTest.name);//编译错误
}
}


XKP 2003-06-30
  • 打赏
  • 举报
回复
我的意思是说

public String name="dalily";
变成静态的
public static String name="dalily";

要不然就只能创建一个
staticTest实例,然后引用他的name
因为name是成员
xuancao 2003-06-30
  • 打赏
  • 举报
回复
public class staticTest{
public String name="dalily";
public static void main(String args[]){
staticTest a = new staticTest();
System.out.print(name);//编译错误
System.out.print(staticTest.name);//编译错误
}
}
pleonheart 2003-06-30
  • 打赏
  • 举报
回复
System.out.print(staticTest.name);和System.out.print(this.name);有什么区别?
yuanmeng163 2003-06-30
  • 打赏
  • 举报
回复
main方法是静态的,在方法内是不能使用外部非静态成员变量的。
this是代表当前对象的引用,也就是说这个类相当于有一个叫this的成员变量(指向当前对象的引用),而这个变量不是静态的,所以不能在main方法中使用。
staticTest a = new staticTest();//这里a是在静态方法内声明的,为局部变量,可以使用
XKP 2003-06-30
  • 打赏
  • 举报
回复
System.out.print(staticTest.name);
wanglh2000 2003-06-30
  • 打赏
  • 举报
回复
静态方法可以在类没有创建的时候调用,其中this表示这个类的一个实例。
程序一先创建了一个类的实例a,然后通过a来调用成员变量name,当然没有什么错误。
程序二没有创建一个类的实例,当然this也就无从说起了。静态方法中不能使用this修饰符。
这样修改就行了:
public class staticTest{
public static String name="dalily";
public static void main(String args[]){
//staticTest a = new staticTest();
System.out.print(name);
}
}

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧