File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inner classes Problem

 
Mohit G Gupta
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • 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 ??
 
Neha Daga
Ranch Hand
Posts: 504
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Malatesh Karabisti
Ranch Hand
Posts: 153
Java Oracle
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic