Win a copy of Building Blockchain Apps this week in the Cloud/Virtualization 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

static inner classes/nested classes

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pleas help to clear up some of my confusion on the topic of static inner classes:

1) If the class is not a static class and just a static member why is the syntax "static class Nest {}" used?

2) And if it is just a member does it follow the rule for other static members that only one copy exists for the class?

3) One last thing: it appears as though the class is being instantiated in the example given in the book. Is that possible for a "static" class?

I'm preparing to take the certification exam for Java 6, and I'm trying to clear up my fuzziness on this subject. The information is on pgs. 680-681 of Sun Certified Programmer for Java 6 Study Guide.

I appreciate any help that anyone can provide.

Thanks in advance,

Doug Sherm
 
Sheriff
Posts: 9671
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Doug Sherm wrote:1) If the class is not a static class and just a static member why is the syntax "static class Nest {}" used?


I don't understand this question. Every static member of a class are declared with the static syntax, a static inner class is also a static member of the enclosing class, so why wouldn't you use the static keyword.

Doug Sherm wrote:2) And if it is just a member does it follow the rule for other static members that only one copy exists for the class?


Classes don't have copies, whether there is a static or non-static inner class, there will be one definition of the class loaded by the class loader

Doug Sherm wrote:3) One last thing: it appears as though the class is being instantiated in the example given in the book. Is that possible for a "static" class?


Which example?? Its easier if you show us the example that you are confused with, not everyone has the book which you are referring to or even if they have the book, they might not have it with them everytime...
 
Ranch Hand
Posts: 317
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankit can you please explain the second point a bit more?
 
Ankit Garg
Sheriff
Posts: 9671
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Harpreet to the JVM inner classes are same as top level classes. So each instance of a class doesn't have an associated instance of any non-static inner classes. So whether an inner class is static or non-static, the JVM will only load the class once. After that we can create multiple instances of the inner class. The only difference between static and non-static inner classes is that non-static inner classes have an associated instance of the enclosing class, that's why they can access non-static members of the enclosing class while static inner classes cannot directly access non-static members of the enclosing class (they need to instantiate the enclosing class and access the non-static members on that instance)...
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Doug Sherm wrote :


1) If the class is not a static class and just a static member why is the syntax "static class Nest {}" used?



You mean, In K&B's book, It is said that,


The class itself isn't really "static"; there's no such thing as a static class............


 
Harpreet Singh janda
Ranch Hand
Posts: 317
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the explanation Ankit
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Doug Sherm,

Have a look on Javaworld link
 
Thank you my well lotioned goddess! Here, have my favorite tiny ad!
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!