File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inner class-final static variable is possible

 
Anuji Philip
Ranch Hand
Posts: 46
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 61
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Anuji Philip
Ranch Hand
Posts: 46
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 46
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
Anuji Philip
Ranch Hand
Posts: 46
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2120
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jose Botella
Ranch Hand
Posts: 2120
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 46
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A great answer.I can now understand byte code also little.A good step
towards a better understanding.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic