This page contains an archived post to the Design Forum (formerly called the Flexible Java Forum) made prior to February 25, 2002.
If you wish to participate in discussions, please visit the new
Artima Forums.
Message:
static final fields and static initializers
Posted by Thierry Bezecourt on 22 May 1998, 2:39 AM
I am reading your April article in JavaWorld. You write that : "References to static final variables initialized to a compile-time constant are resolved at compile-time to a local copy of the constant value". So, apparently, the static initializer is not called when a final static member is used. The sample code below illustrates that. Don't you think it might be confusing ? Sample code : public class FinalTest { public static void main(String argv[]) { System.out.println("final = "+Loaded.x); // The static initializer of Loaded in the following line : System.out.println("non-final = "+Loaded.y); } } class Loaded { static { System.out.println("\tInitialization of Loaded"); } public static final String x = "final"; public static String y = "non-final"; }
Replies:
|