testconst.java
来自「java安全性编程实例电子版书附源码,是网络安全编程者不可多得的好书」· Java 代码 · 共 40 行
JAVA
40 行
public class TestConst{
//有问题的定义
final int x[ ]={1,2,3};
//无问题的定义
final NewConst y=new NewConst();
public static void main(String args[] ){
TestConst t=new TestConst( );
//执行go( )方法试图修改常量x和y中的内容
t.go( );
//查看常量x的内容
int[ ] x1=t.x;
System.out.println("The code with problem");
for(int i=0;i<x1.length;i++){
System.out.println(x1[i]);
}
//查看常量y的内容
NewConst x2=t.y;
System.out.println("The code without problem");
for(int i=0;i<x2.getConst().length;i++){
System.out.println(x2.getConst()[i]);
}
}
void go(){
//试图修改常量x和y中的内容
x[0]=20;
y.getConst()[0]=20;
}
}
class NewConst{
private final int x[ ]={1,2,3};
public final int[ ] getConst( ){
int [] s=(int []) x.clone();
return(s);
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?