Thursday, January 17, 2008

 

When is a Constant not a Constant?

When it's a java object.

Oh, sure, you can declare a final object. What that means is that you can't change the reference. But values are up for grabs.
I wonder how many people this has torched in the past?

Thank god for SCJP where I learned that final is not final as in primitives final but somewhat less final than you would sometimes want.

I guess the next thing to find out is how you keep an object from changing its values (or rather, guarantee that it can't).

And final doesn't always mean the same thing. Final in a primitive means you can't change the value. Final in an object means you can't change the reference. Final in a method means that you can't override the method on inheritance. Final in a class means you can't inherit from it at all.

Interesting rules, and it doesn't seem like final is ever misused but it can be confusing. At least to me, anyway.


Powered by ScribeFire.


Comments: Post a Comment



<< Home

This page is powered by Blogger. Isn't yours?