• Post Reply Bookmark Topic Watch Topic
  • New Topic

local inner class in a static method  RSS feed

 
luke brown
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is it necessary that inner classes inside a static method be static . If yes Why?
 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is it necessary that inner classes inside a static method be static.


Must not be that difficult to try. You need only create a class and a static method and inside that static method you need create another class.

So when you do that, does the compiler allow you to do it? More on that later.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think about it … members of a class can be static or otherwise. Are things local members of the class?
 
luke brown
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i did some work around:


that runs fine (though i have no message to display) . above i was checking-
>non static inner class must not have static members since its members are poited with the help of outer class object as reference. thats fine.
>My lecturer stated that - inner class inside a static method are static by default. i was wondering how. never got the logic . eventually the above code proved that wrong too. but still i am unsure of reasoning.
 
Chan Ag
Rancher
Posts: 1090
14
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is it necessary that inner classes inside a static method be static


Inner classes inside a static method or for that matter any other method cannot be static. Method local means it should only be local to that method which static classes by their nature are not.





that runs fine (though i have no message to display) .


That runs fine cause the method local class inside the static method is not static. In fact the only modifiers allowed for the method local inner classes are 'abstract' and 'final'.

My lecturer stated that - inner class inside a static method are static by default. i was wondering how. never got the logic . eventually the above code proved that wrong too. but still i am unsure of reasoning.


I am not too sure about what he meant. So I will have to read what others think about that.


 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!