aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Inner class-final static variable is possible Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Inner class-final static variable is possible" Watch "Inner class-final static variable is possible" New topic
Author

Inner class-final static variable is possible

Anuji Philip
Ranch Hand

Joined: Feb 25, 2002
Posts: 46
In an inner class static variable is not possible as only members of
interface and top level classes can be static .But static final is possible.
why so?
aymen esawey
Ranch Hand

Joined: Jan 29, 2002
Posts: 61
answer from JLS(I'm possessed by Val)

class Outer{
class Inner extends HasStatic{
static final x = 3;// ok - compile-time constant
static int y = 4; // compile-time error, an inner class
}
static class NestedButNotInner{
static int z = 5; // ok, not an inner class
}
interface NeverInner{} // interfaces are never inner
}

[ March 15, 2002: Message edited by: aymen esawey ]

Aymen Esawey<br />SCJP <img src="smile.gif" border="0"> <br /><a href="http://www.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=32&t=001968" target="_blank" rel="nofollow">how to nuke the SUN </a>
Anuji Philip
Ranch Hand

Joined: Feb 25, 2002
Posts: 46
If only compile time constants valid inside inner class what about
int y = Math.random(); this?Please give
a detatiled explanation
Anuji Philip
Ranch Hand

Joined: Feb 25, 2002
Posts: 46
a
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
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)


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Anuji Philip
Ranch Hand

Joined: Feb 25, 2002
Posts: 46
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?
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Hello Anuji
Your first post is answered by JLS 8.1.2

Inner classes may not declare static members, unless they are compile-time constant fields (�15.28).

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
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
A great answer.I can now understand byte code also little.A good step
towards a better understanding.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Inner class-final static variable is possible