Constant Object vs Immutable Object
Can I use the term "Constant Object" in the place of the term "Immutable Object"? Though I get the feeling that Immutable for an Object is what Constant is for a variable, I am not sure if this terminology is accepted. Please help me underst开发者_StackOverflowand.
In fact, in Java the term constant has no defined meaning. It occurs in the JLS only in the larger term compile time constant expression, which is an expression which can (and must) be calculated by the compiler instead of at runtime. (And the keyword const
is reserved to allow compilers to give better error messages.)
In Java, we instead use the term final to refer to variables (whether class, object or local ones) who can't be changed, and immutable when we refer to objects who can't change. Both can be used when speaking about a variable x
- the first then means the variable itself (meaning it can't be switched to another object), the second means the object behind the variable. Thus here the two meanings are orthogonal, and are often combined to create a "true constant".
I would read constant as being the same object (the same reference), whereas immutable clearly means to me the fact that the object doesn't change.
Since these are two different things, I would perhaps refer to this:
private final Immutable i = new Immutable();
as being a constant immutable object.
Constant often has a very specific meaning in different programming languages. In java, a constant already refers to a constant variable, a variable that cannot be changed after assignment, e.g.:
final int FOO = 1;
FOO = 4; // constant variable cannot be changed.
An immutable object is a constant object in the sense that its properties can never be changed, but of course an object pointed to by a constant variable can still be changed. So to avoid confusion, the term immutable (which literally means "unchanging over time") is used.
They are very close in meaning, with the understanding that an Object contains methods while a Constant is generally considered to only contain data.
Within Java, there's the additional consideration of the keyword final
, which basically means non-reassignable. Some people will casually call a final variable a constant (as it's reference to a particular object is a constant. This often comes about due to confusion as to the particular roles of the member and the object it refers to, as 95% of the time a person does this to refer to an immutable Object.
Not every method is to return back data that depends wholly upon the internal members. For example System.currentTimeMillis()
returns back a Unix like timestamp, yet there would be no need for the actual "System" object to change.
Immutability of the object means it can't transform it's state... i.e. can't mutate... For examle
final class Person {
private int age = 0;
public Person(int age) { this.age = age; }
}
The object to this type are immutable objects since you can't change it's state.... (forget Reflection for a moment)
Constant at the other hand in Programming means inline... Even the compiler does that they inline the values of the constant variables for primative types... for object types it means the ref variable can't be reassigned.
精彩评论