This week's book giveaway is in the OCAJP forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide 1Z0-808 and have Jeanne Boyarsky & Scott Selikoff on-line! See this thread for details.
hi! can a non-static inner class contain a final static variable which is a compile time constant the Dan's mock says yes. but then we should be able to access it like new Outer().NonStaticInner.e); but this gives error instead this works. new Outer().new NonStaticInner().e); why! also can a nested interface have accesibility modifiesrs. madhur.
Originally posted by madhur jain: also can a nested interface have accesibility modifiesrs.
I think it can have any accessibility: �9.1.1 Interface Modifiers "The access modifier public is discussed... The access modifiers protected and private pertain only to member interfaces within a directly enclosing class declaration." Please correct me if I mis-read the JLS for the 2nd question
Originally posted by madhur jain: hi! but then we should be able to access it like new Outer().NonStaticInner.e); but this gives error instead this works. new Outer().new NonStaticInner().e); why! madhur.
Yes, the first syntax example does not work but the second one does. It is necessary to create an instance of the NonStaticInner class before attempting to access any member of the class. Since the constant value that you are attempting to access does exist at compile time it would have been possible to design the language such that the compile time constant can be accessed without first creating an instance of the NonStaticInner class. It appears that James Gosling didn't feel that it was worth the trouble.
Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Madhur, Inner classes may have compile constants. That you already know. But the inner class is not static - 'NonStaticInner'. So you cannot access it directly since It is not in the scope of a static method of the enclosing class. Another anology - class instance variables cannot be accessed directly from static methods.