• 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

Can an Inner class be static?

 
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Enthuware


Which of the given options if put at //1 will correctly instantiate objects of various classes defined in the following code?

Options:
1. new TestClass().new A();
2. new TestClass.B();
3. new A();
4. new TestClass.A();
5. all of them
Answer is 5.

But when I try this in eclipse it giver me compiler error st static class B that "this modifier can not be applied to class".
Can any one help me please? can we declare inner class static?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Janki Shah wrote:

Enthuware


Which of the given options if put at //1 will correctly instantiate objects of various classes defined in the following code?

Options:
1. new TestClass().new A();
2. new TestClass.B();
3. new A();
4. new TestClass.A();
5. all of them
Answer is 5.

But when I try this in eclipse it giver me compiler error st static class B that "this modifier can not be applied to class".
Can any one help me please? can we declare inner class static?




Yes. Inner classes which are declared static are called nested classes. Can you show us the code which caused eclipse to choke?

Henry
 
Janki Shah
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the code which gives me compiler error."Illegal modifier for the local class. only abstract or final is permitted."
 
Ranch Hand
Posts: 820
IntelliJ IDE VI Editor Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Janki Shah wrote:Here is the code which gives me compiler error."Illegal modifier for the local class. only abstract or final is permitted."



you have an extra bracket at line 3 so that it looks like you are trying to make an anonymous code block (but you don't have a closing bracket for this one ). Not sure if this was intentional, but static nested classes aren't allowed inside anonymous code blocks because anonymous code blocks are run at object creation and a static context makes no sense at that time How would you call it? Remove the extra bracket.
 
Janki Shah
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oooh! Thanks Tim. Sometimes silly syntax error takes so much time.
reply
    Bookmark Topic Watch Topic
  • New Topic