class text0{
private static long counter=0;
private final long id=counter++;
public text0(){
System.out.println("id="+id+" counter"+counter);
}
}
public class Damo0 { public static void main(String[] args) {
new text0();
new text0();
new text0();
new text0();
new text0();
new text0();
new text0();
}}
output:id=0 counter1
id=1 counter2
id=2 counter3
id=3 counter4
id=4 counter5
id=5 counter6
id=6 counter7
final不是只可以赋常量值吗?
private static long counter=0;
private final long id=counter++;
public text0(){
System.out.println("id="+id+" counter"+counter);
}
}
public class Damo0 { public static void main(String[] args) {
new text0();
new text0();
new text0();
new text0();
new text0();
new text0();
new text0();
}}
output:id=0 counter1
id=1 counter2
id=2 counter3
id=3 counter4
id=4 counter5
id=5 counter6
id=6 counter7
final不是只可以赋常量值吗?
id的值赋值之后并没有变,变的是counter
你可以试试改变id, 一定会报错