• 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 classes Problem

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



getting error:

inner classes cannot have static declarations
static void Bunkamethod1()
^
1 error


why are inner classes not allowed static declaration ??
 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lets look at this way, the inner class is a member ( instance) variable (assume like that not the same) for the outer class. So it is like if you are declaring the instance methos in a class. Now think, can you declare a static variable inside the method?
So just like inside the instance declaration of a inner class you cannot declare static variables.
If you want to, declare inner class as static.
 
Ranch Hand
Posts: 153
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We appreciate the static members because they are available to us as ready to use without creating the new instance of the of the enclosing class i.e without the headache of "new"

Let assume we have class called "Outer" and which has non static inner class called "Inner".
Is there way to access the members of "Inner" class without crating the instance of Outer class ?
No! we can't access them.

If we can't access the members of "Inner" class without creating instance of "Outer" class. then what is the use if we have the static member in "Inner" class. hence compiler won't allow us have static members in non static inner class.

I hope that helps.
reply
    Bookmark Topic Watch Topic
  • New Topic