Win a copy of Head First Android this week in the Android forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

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.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic