Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java SE 综合讨论区 » Java与OOP初步  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 final 关键字的含义(source code from "think in java chp06")
eqq2002





发贴: 22
积分: 0
于 2006-02-07 10:17 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
I am now studying tij 3rd. In CHP06, the keyword "final" make me confused.
1.what I am not very clear is the composition of "private + static + final".
what is "static" meaning here? what is "final" meaning?
I used to insert "final" before primitives and consider it reasonable, for example" final int k = 2006",
2. but,when final is before array( reference mentioned in think in java), I fell into
misunderstanding.

source code is below:

//: c06:FinalData.java
// The effect of final on fields.
import com.bruceeckel.simpletest.*;
import java.util.*;

class Value {
int i; // Package access
public Value(int i) { this.i = i; }
}

public class FinalData {
private static Test monitor = new Test();
private static Random rand = new Random();
private String id;
public FinalData(String id) { this.id = id; }
// Can be compile-time constants:
private final int VAL_ONE = 9;
private static final int VAL_TWO = 99;
// Typical public constant:
public static final int VAL_THREE = 39;
// Cannot be compile-time constants:
private final int i4 = rand.nextInt(20);
static final int i5 = rand.nextInt(20);
private Value v1 = new Value(11);
private final Value v2 = new Value(22);
private static final Value v3 = new Value(33);
// Arrays:
private final int[] a = { 1, 2, 3, 4, 5, 6 };
public String toString() {
return id + ": " + "i4 = " + i4 + ", i5 = " + i5;
}
public static void main(String[] args) {
FinalData fd1 = new FinalData("fd1");
//! fd1.VAL_ONE++; // Error: can't change value
fd1.v2.i++; // Object isn't constant!
fd1.v1 = new Value(9); // OK -- not final
for(int i = 0; i < fd1.a.length; i++)
fd1.a[i]++; // Object isn't constant!
//! fd1.v2 = new Value(0); // Error: Can't
//! fd1.v3 = new Value(1); // change reference
//! fd1.a = new int[3];
System.out.println(fd1);
System.out.println("Creating new FinalData");
FinalData fd2 = new FinalData("fd2");
System.out.println(fd1);
System.out.println(fd2);
monitor.expect(new String[] {
"%% fd1: i4 = \\d+, i5 = \\d+",
"Creating new FinalData",
"%% fd1: i4 = \\d+, i5 = \\d+",
"%% fd2: i4 = \\d+, i5 = \\d+"
});
}
} ///:~

thanks

如果该贴中有违反发贴规范之处,请指出,定整改。


why edited on 2006-02-07 12:16


关于Jute Forum的长期发展给官方的建议

话题树型展开
人气 标题 作者 字数 发贴时间
8751 final 关键字的含义(source code from "think in java chp06") eqq2002 2381 2006-02-07 10:17
6362 Re:final 关键字的含义(source code from "think in java chp06") wkz19820223 16 2006-02-08 16:31
6553 Re:final 关键字的含义(source code from "think in java chp06") java8964 2091 2006-02-09 00:08
6251 Re:final 关键字的含义(source code from "think in java chp06") bukaoyan 20 2006-02-12 12:57
6665 Re:final 关键字的含义(source code from "think in java chp06") ranchgirl 766 2006-02-13 00:32

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923