Win a copy of Classic Computer Science Problems in Swift this week in the iOS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

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: 1794
45
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!