Yes this is valid, try to write some code and you'll see that it works... a random double value will be stored in i provided you change the statement to: public static final double i = Math.random(); //(not int)

Oh..........My question was a blunder.The point I donot know was what is the difference between static final double D = Math.random() and static double e = Math.random(). Here D is not a compile time constatnt.But changes in each run time invocation.But from object to object it doesn't.But for that only static, not static final is required.Why?

Also "public static final double i = Math.random();" is not a valid member of an inner class because the invocation of a method is not included in JLS 15.28 To see why only static final fields, that are initialized to a compile constant (JLS 15.28), are the only static valid members for an inner class look at the output of javap for the following code.

In line 3 the value 3 is passed to the print method via an iconst_3 bytecode instead of looking for the content of the field x. Each time an access to such field ocurrs iconst_3 will be used. Why? because it is not possible for an inner class to hold a static field. Similarly in line ten, the value 58888 is passed to the print method via a ldc #5 bytecode. Given that the value of y is known at compile time as constant, compiler doesn't use a bytecode that access the value of a field y that is static, and thefore not possible in an inner class. Compiler uses a ldc #5 bytecode each time the value of y is needed. This bytecode gets the value (58888) that is stored in the entry 5 in the constant pool of the class Outer.Inner. It follows that the values from compile constants are retrived in ways that avoid the use of fields in the object. They are stored in the constant pool or are assigned directly by a bytecode like iconst_3. Thus static fields in inner classes are not created.

SCJP2. Please Indent your code using UBB Code

Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120

posted Mar 17, 2002 10:23:00

0

Ihave found the following in JLS 12.4.1

* References to compile-time constants must be resolved at compile time to a copy of the compile-time constant value, so uses of such a field never cause initialization.

Anuji Philip
Ranch Hand

Joined: Feb 25, 2002
Posts: 46

posted Mar 18, 2002 00:47:00

0

A great answer.I can now understand byte code also little.A good step towards a better understanding.