It's not a secret anymore!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes non-static inner class and final static var. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "non-static inner class and final static var." Watch "non-static inner class and final static var." New topic
Author

non-static inner class and final static var.

swapna sivaraju
Ranch Hand

Joined: Jan 18, 2002
Posts: 75
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


SCPJ2
Valentin Crettaz
Gold Digger
Sheriff

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

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

Joined: Apr 11, 2001
Posts: 356
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

Joined: Jan 18, 2002
Posts: 75
Thank u both..u cleared my doubt.
swapna
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: non-static inner class and final static var.
 
Similar Threads
A question about Inner Class
member interface
Inner Interfaces vs Nested Interfaces
non static inner class.
Inner class