• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Static class

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2908
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
There's a city wid manhunt for this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic