我为什么可以改变final数据

zhch200 2004-07-29 04:49:10
import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class Applet1 extends Applet {

// List mylist;
// Label listStatus;

public Applet1() {
}

public void init() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}

private void jbInit() throws Exception {
final List mylist= new List(5,true);
//////////////////
final Label listStatus = new Label("this is this list status");
//////////////////
mylist.add("metre");
mylist.add("thiste");
mylist.add("herde");
mylist.add("htidfj");
mylist.add("string");

this.add(listStatus);
this.add(mylist);
TextField mytext = new TextField("thisf",10);
this.add(mytext);
mylist.addItemListener( new ItemListener(){
public void itemStateChanged(ItemEvent evt){
String selection;
int selectionNum;
if(evt.getStateChange()== ItemEvent.SELECTED)
{
selectionNum = ((Integer) evt.getItem()).intValue();
selection = mylist.getItem(selectionNum);
////////////////////////////////
listStatus.setText("you select is "+selection);
////////////////////////////
}

}
});
}
}
在////////////////中的数据为fianl
我如果将final去掉.程序出错
说内部类访问的数据要设置为final
我加拉fianl 程序运行正常,listStatus显示的数据可以修改
...全文
181 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
umbrella1984 2004-07-29
  • 打赏
  • 举报
回复
你那没有改变引用的对象,只是修改原来对象。
registered 2004-07-29
  • 打赏
  • 举报
回复
anonymous inner class 只能使用 final 局部变量
但是有 outer class 成员的使用权(无限制)
zhch200 2004-07-29
  • 打赏
  • 举报
回复
import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class Applet1 extends Applet {

List mylist;
Label listStatus;

public Applet1() {
}

public void init() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}

private void jbInit() throws Exception {
mylist= new List(5,true);
//////////////////
listStatus = new Label("this is this list status");
//////////////////
mylist.add("metre");
mylist.add("thiste");
mylist.add("herde");
mylist.add("htidfj");
mylist.add("string");

this.add(listStatus);
this.add(mylist);
TextField mytext = new TextField("thisf",10);
this.add(mytext);
mylist.addItemListener( new ItemListener(){
public void itemStateChanged(ItemEvent evt){
String selection;
int selectionNum;
if(evt.getStateChange()== ItemEvent.SELECTED)
{
selectionNum = ((Integer) evt.getItem()).intValue();
selection = mylist.getItem(selectionNum);
////////////////////////////////
listStatus.setText("you select is "+selection);
////////////////////////////
}

}
});
}
}
那为什么放在外部类里申明,不放在函数中申明,程序又运行正常
ntzls 2004-07-29
  • 打赏
  • 举报
回复
匿名内部类访问的外部数据必须要设置为final

final listStatus指向新生成的Label对象,不能对其重新赋其他Label对象。

当然此时final listStatus所指向的Label对象可通过相应方法更改其自生属性。
zhch200 2004-07-29
  • 打赏
  • 举报
回复
那为什么 去掉final 之后程序报错
yeahnope 2004-07-29
  • 打赏
  • 举报
回复
final仅仅表明一旦它指向一个对象, 它就不能再指向其他对象.
zhch200 2004-07-29
  • 打赏
  • 举报
回复
那为什么 去掉final 之后程序报错
ChDw 2004-07-29
  • 打赏
  • 举报
回复
final声明的是mylist的指向不可以改变,即
不再可以出现mylist = ...这种语句

62,623

社区成员

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

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