Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

non-static inner class and final static var.

 
swapna sivaraju
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys..
Check this code..
class Another
{
}
class outer
{
class inner
{
final static int z=100;//1
final static Another an=new Another();//2
}
}
In this code 1 is ok but 2 generates a compiler error.
1 is okay as 'z' is a compile time constant but what abt line 2.???
iam right-----since will create the object at runtime so at compile time its still a static var. and since non-static inner classes cant declare static vars..this generates an error!
thanks
swapna
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From JLS 8.1.2 Inner Classes and Enclosing Instances

An inner class is a nested class that is not explicitly or implicitly declared static. Inner classes may not declare static initializers (�8.7) or member interfaces. Inner classes may not declare static members, unless they are compile-time constant fields (�15.28).

From JLS 15.28 Constant Expression

A compile-time constant expression is an expression denoting a value of primitive type or a String...

[ March 11, 2002: Message edited by: Valentin Crettaz ]
 
Vanitha Sugumaran
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
final static Another an=new Another();
is not a compile-time constant expression, that's why you get error.
Take a look at,
JLS 15.28 Constant expressions.
http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#5313
Hope this helps,
Vanitha.
 
swapna sivaraju
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank u both..u cleared my doubt.
swapna
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic