Topic: final 关键字的含义(source code from "think in java chp06")

  Print this page

1.final 关键字的含义(source code from "think in java chp06") Copy to clipboard
Posted by: eqq2002
Posted on: 2006-02-07 10:17

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

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

2.Re:final 关键字的含义(source code from "think in java chp06") [Re: eqq2002] Copy to clipboard
Posted by: wkz19820223
Posted on: 2006-02-08 16:31

用中文提问可能会大的人会多点Smile

3.Re:final 关键字的含义(source code from "think in java chp06") [Re: eqq2002] Copy to clipboard
Posted by: java8964
Posted on: 2006-02-09 00:08

Since you ask this question in English, I will try to answer it in English.

The final keyword in Java has different meanings when it is used in different way. It really depends on the context. But in general, it means 'Don't change'.

Most programming languages have a way to tell the compiler that some data is "constant". A constant data means in two way:
1) It can be a compile-time constant that will NEVER change
2) It can be a value initialized at run-time and you don't want it changed after that.

At compile time constant, normally means its value can be determined at compile time. So it normally means the primitive types of Java, like int. A value must be given at time of definition. For example:

static final int SUNDAY = 0;

// SUNDAY = 1; // can't compile since you try to change a constant value

The private and public has its own meanning, used with static or final really didn't change its meaning. Private just means this constant is private, only available for the class it is defined into. Public means it is public, anyone can use it.

Your confusion of final used in front of array is that you are not clear what it means when the final key word is used with object handlers rather than primitives. With a primitive, final makes the value a constant, but with an object handle, final makes the handler a constant. What it means is that when the handle (or reference) must be initialized to an object at the point of declaration, and the handle can never be changed to point to another object.
The important thing is that the object itself can be modified. This restriction includes arrays, which are also objects.

For example:

final int[] a = {1, 2, 3, 4};

// a = new int[6] // invalid, you can't reassign a to another object
// a[0] = 2 // fine, you can change the value

The above is called final data. The final keyword of Java is also used as
1) Final arguments
2) Final method
3) Final class

All of them have their own meanings. "Thinking in Java" clearly explains them. Just read them, if you have more confusion, 欢迎和我探讨。

4.Re:final 关键字的含义(source code from "think in java chp06") [Re: eqq2002] Copy to clipboard
Posted by: bukaoyan
Posted on: 2006-02-12 12:57

好英文啊
羡慕!
我看的懂但写不出来

5.Re:final 关键字的含义(source code from "think in java chp06") [Re: eqq2002] Copy to clipboard
Posted by: ranchgirl
Posted on: 2006-02-13 00:32

final variables cannot be changed.
final methods cannot be overridden.
final classes cannot be inherited.

However, there is something easily to be confused. There is no final Object in Java. You cannot even declare an Object in Java, only Object references. We only can have final object reference, which cannot be changed. It is like that the address of your house cannot be changed if it is declared as final. However, you can remodel your house, add a room, etc. Your house is not final, but the reference to your house is final. That is a little confusing. By the way, use finals as much as possible, but do not overuse them of course. It is because compiler knows they are not going to change, the byte code generated for them will be much more efficient.


   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