static final修饰不明白

wangdong20 2013-05-01 12:31:58
声明的变量
public final static int[][] highscores = new int[3][5];
public final static String[][] players = new String[][]{
{"player", "player", "player", "player", "player"},
{"player", "player", "player", "player", "player"},
{"player", "player", "player", "player", "player"},
};

对其进行如下操作
public static void addScore(int diff, String player, int score){
for(int i = 0; i < 5; i++){
if(score > highscores[diff - 3][i]){
for(int j = 4; j > i; j--){
highscores[diff - 3][j] = highscores[diff - 3][j - 1];
players[diff - 3][j] = players[diff - 3][j - 1];
}
highscores[diff - 3][i] = score;
players[diff - 3][i] = player;
break;
}
}
}

为什么没报错,按理说final修饰后应该不能修改
...全文
222 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
wangdong20 2013-05-01
  • 打赏
  • 举报
回复
引用 1 楼 huntor 的回复:
final 修饰一个引用类型,指的是不能对变量重新赋值,但变量本身可以变化。
这句话还是不太明白,举个例子吧
huntor 2013-05-01
  • 打赏
  • 举报
回复
final 修饰一个引用类型,指的是不能对变量重新赋值,但变量本身可以变化。
ntwarren 2013-05-01
  • 打赏
  • 举报
回复
先说一下,其实引用这个词用在JAVA上不太合适,因为在C++中引用的本来就是一个常量,一旦指向对象,便不能再指向其它的对象。 好了,对于这个问题,你只要理解JAVA中FINAL在修饰变量的时候有两种情况 1.对于基本类型,一旦被FINAL修饰,他就是一个常量。 final int i =8; //can't do this: //i=8; 2.对于对象类型,一旦被FINAL修饰,对象的"指针"但不能再指向其它的对象。 class A{int b = 0;} final a = new A(); //can't do this: //a = new A(); //but you can do this: a.b = 3; 也就是说JAVA没有提供常量对象的机制,而在C++中是有这种机制的。
sxax 2013-05-01
  • 打赏
  • 举报
回复
引用 2 楼 wangdong20 的回复:
[quote=引用 1 楼 huntor 的回复:] final 修饰一个引用类型,指的是不能对变量重新赋值,但变量本身可以变化。
这句话还是不太明白,举个例子吧[/quote] final int i=1; i=8; 直接错,final修饰的变量不可以被修改
xianwangkai 2013-05-01
  • 打赏
  • 举报
回复
地址可以不可以变,但是值可以变,也就是说不能重新赋值的概念!
wangdong20 2013-05-01
  • 打赏
  • 举报
回复
引用 9 楼 cai5 的回复:
比如
public final static int[][] highscores = new int[3][5];  

public void xXXX(){
  int [][] A = new int[3][5] ;
  ...
  highscores = A ; //这样子是错误的,highscores 指向的对象在内存中的地址不能改变 
  highscores[0][0] = 1 ;//合法 ,赋值
}
引用 8 楼 cai5 的回复:
public final static int[][] highscores = new int[3][5]; 
    public final static String[][] players = new String[][]{ 
        {"player", "player", "player", "player", "player"}, 
        {"player", "player", "player", "player", "player"}, 
        {"player", "player", "player", "player", "player"}, 
    };
这的final修饰的是 highscores这个变量 ,它指向的内存地址不能变,但是他内存地址存放的值是可以改变的
了解了,讲的很清楚
五哥 2013-05-01
  • 打赏
  • 举报
回复
比如
public final static int[][] highscores = new int[3][5];  

public void xXXX(){
  int [][] A = new int[3][5] ;
  ...
  highscores = A ; //这样子是错误的,highscores 指向的对象在内存中的地址不能改变 
  highscores[0][0] = 1 ;//合法 ,赋值
}
五哥 2013-05-01
  • 打赏
  • 举报
回复
public final static int[][] highscores = new int[3][5]; 
    public final static String[][] players = new String[][]{ 
        {"player", "player", "player", "player", "player"}, 
        {"player", "player", "player", "player", "player"}, 
        {"player", "player", "player", "player", "player"}, 
    };
这的final修饰的是 highscores这个变量 ,它指向的内存地址不能变,但是他内存地址存放的值是可以改变的
u010525610 2013-05-01
  • 打赏
  • 举报
回复
final是常量,修饰后不可以修改
lcf 2013-05-01
  • 打赏
  • 举报
回复
final只是对该变量本身进行保护,而并不是对其引用的对象进行保护。换句话说,Java不会检测你是否对其引用的对象进行了改变,而只是很简单地检测你是否对该变量进行了重新赋值。 PS: 关于final的另一个知识点 如果C++里面用const关键字去修饰一个类的成员函数,C++的编译器会检测这个函数是否更改了这个类的状态,也就是对类成员的写入。Java就没有这么神奇的功能了。final修饰一个函数仅仅代表它不能被子类重写而已
wangdong20 2013-05-01
  • 打赏
  • 举报
回复
引用 3 楼 huan_ji 的回复:
那是一个常引用变量,不能改变它所引用的对象
是不是声明highscores后,再次调用highscores = new int[2][2];会报错 player也是player = new String[2][2]
fei1710 2013-05-01
  • 打赏
  • 举报
回复
不能再赋值另外一个对象,对象都是引用关系
huan_ji 2013-05-01
  • 打赏
  • 举报
回复
那是一个常引用变量,不能改变它所引用的对象

62,614

社区成员

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

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