Main BLOGGER
Google
WWW THIS BLOG
Wednesday, May 02, 2007
 
Java series in IBM developer works
http://www-128.ibm.com/developerworks/views/java/libraryview.jsp?search_by=practice:


--
Pop (Pu Liu)

 
inner class and static members


Registered: 9/7/06
Inner class and static members   
Apr 25, 2007 1:25 AM

 
Hi, I know that a non-static inner class can not have static members. But the following code compiles successfully and I am confused at it.

class MyOuter
{
MyInner inner = new MyInner();

   class MyInner
   {
      //private static int[] arr1 = new int[2];
      //private static int a1 = 5;
      //private static final int[] arr2 = new int[2];
      //private static final String str = foo();  //compiling error
       private static final int a2 = 5;
    }
}

This class compiles successfully though it does not do anything. Here a2 has been declared both static and final and compiler does not object to that whereas if I uncomment any of the commented declarations, the compiler shows errors. Please tell me what is the problem over here.
 
YoGee
Posts:4,383
Registered: 6/19/02
Re: Inner class and static members   
Apr 25, 2007 1:39 AM (reply 1 of 2)

 
The rule is that inner classes may not declare static members, unless they are compile-time constant fields.

http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#5313
 
maity
Posts:33
Registered: 9/7/06
Re: Inner class and static members   
Apr 30, 2007 12:47 AM (reply 2 of 2)

 
Thanks a lot YoGee.

It was great to learn the fact that compile-time constants can be declared in non-static classes.


--
Pop (Pu Liu)


Powered by Blogger

Google
WWW THIS BLOG