• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

inner class

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic