• 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:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Static class  RSS feed

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I have some confusion related to static class. I have read in a book that

A static nested class is not an inner class, it's a top-level nested class.



but we code it like this...



Nested is defined within BitOuter, then how can one say that it is not an inner class.

 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because it shares absolutely no relationship with it's enclosing class. It is treated (instantiated) as if it was a top level class.
 
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They don't consider it an inner class because inner classes share a "special" relationship with its outer class, because you need a live instance to instantiate it, whereas with a static class you don't. It's scope is nested but its access it top-level.

 
Bartender
Posts: 1845
46
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Defination:


What is an inner class ?

A Non Static Nested class is known as an inner class.
 
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A static inner class cannot be an inner class in its true meaning because an inner class would closely relate, have a special relationship, to the instances of the outer class created; and it is evident that static has nothing to do with instances in particular.
 
Ranch Hand
Posts: 449
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
quote from Effective Java

Each instance of a nonstatic member class is implicitly associated with an
enclosing instance of its containing class. Within instance methods of a
nonstatic member class, you can invoke methods on the enclosing instance
or obtain a reference to the enclosing instance using the qualified this construct



A static nested class does not have access to the enclosing instance.
 
Himanshu Kansal
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Muhammad Ali Khojaye wrote:quote from Effective Java

Each instance of a nonstatic member class is implicitly associated with an
enclosing instance of its containing class. Within instance methods of a
nonstatic member class, you can invoke methods on the enclosing instance
or obtain a reference to the enclosing instance using the qualified this construct



A static nested class does not have access to the enclosing instance.



That would indeed be the most appropriate explanation.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!