Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Java in General and the fly likes Why non-static inner class cannot have static member Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Why non-static inner class cannot have static member" Watch "Why non-static inner class cannot have static member" New topic
Author

Why non-static inner class cannot have static member

siddharth das
Ranch Hand

Joined: Aug 17, 2007
Posts: 124
Hi,
Can you please tell why Why non-static inner class cannot have static member?

Thanks,
Siddharth
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14351
    
  22

This has to do with the same thing as what I answered in your other topic. An instance of a non-static nested class is associated with an instance of its enclosing class.

The body of a non-static nested class is not a static scope, and therefore you can't have static members in there. In other words, the member variables and methods in the non-static nested class are relative to an instance of the enclosing class - they are not really at class level (where you could use static). It's a bit hard to explain, but if you've understood my answer in your other topic, I hope you'll understand this as well.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why non-static inner class cannot have static member