aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes inner class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "inner class" Watch "inner class" New topic
Author

inner class

madhur jain
Ranch Hand

Joined: Aug 27, 2002
Posts: 49
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.
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

Originally posted by madhur jain:
can a non-static inner class contain a final static variable which is a compile time constant
the Dan's mock says yes.

The JLS says yes too: �8.1.2 Inner Classes and Enclosing Instances "Inner classes may not declare static members, unless they are compile-time constant fields"
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
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
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>
Vin Kris
Ranch Hand

Joined: Jun 17, 2002
Posts: 154
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: inner class