• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Regular Inner Class

 
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi everyone
I want to know:

1. Why it is that we can not define anything static inside the regular inner class (not talking about the static inner class).
2. Why it is that we can not make an instance of inner class inside it.
 
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tarun Oohri wrote:
1. Why it is that we can not define anything static inside the regular inner class (not talking about the static inner class).


We can define static variables but then they must be final.


Tarun Oohri wrote:
2. Why it is that we can not make an instance of inner class inside it.

Inside what? We can make instances of the inner class.

 
Marshal
Posts: 79151
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tarun Oohri wrote: . . . the static inner class . . .[

You should call that a static nested class. I presume you are familiar with the Java Tutorials section about nested classes.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tarun Oohri wrote:1. Why it is that we can not define anything static inside the regular inner class (not talking about the static inner class).


I suspect it's to keep things simple, although there may also be some structural reasons. But think of it this way: Why do we define static fields to begin with? Generally so that we have something that is globally accessible, either from inside a class (if it's private) or the public at large (if it's public).

Since nested classes are fully visible to their enclosing class, you can't hide a static field by putting it in an inner class, and since there's only ever going to be 1 copy of it, it really doesn't matter whether you define it in the inner class or it's outer one.
There might, however, be a reason for defining a static constant inside an inner class, the most obvious of which is readability - ie, it just makes more sense to name the field in the context of the inner class, because that's where it has a meaning. But, as I hope you know, constants are final.

It may also be worth mentioning that true inner classes (ie, non-static nested classes) are really quite rare, and are usually used to hide an internal implementation, so they're usually private. Some Maps, for example, use them to hold their keys as a Set, which is what you get when you call the keySet() method.

Static nested classes, on the other hand, are much more common, and are just like a regular class - it just happens to be defined inside another one, usually because that's the only place where it makes sense. A good example of a class like that is AbstractMap.SimpleEntry which, as it turns out, implements a nested interface: Map.Entry.

HIH

Winston
 
Master Rancher
Posts: 4796
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think true inner classes are all that rare - but the most common example is anonymous classes, where it's often not that they need to be non-static to access some instance member, but that's just the way the language set up anonymous classes. However named true inner classes aren't all that rare either - most of the collections implementations have at least one, used for an Iterator implementation for example. Because an Iterator is designed to access data in the instance it's iterating, it's a natural use case for an inner class.

I would agree that named inner classes are considerably less common than either static nested classes or anonymous classes. But "really quite rare" seems an overstatement to me.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Simmons wrote:I don't think true inner classes are all that rare - but the most common example is anonymous classes


Yeah, you're right. I should probably have said 'named'. The thing is, I don't really see anonymous classes as 'inner' or 'nested', although I understand that structurally, that's exactly what they are. To me, they're basically a mechanism for creating a temporary object without a lot of hassle.

And I've probably created 10-20 static nested classes for every named non-static one in my programming life; but whether that makes them 'rare' or not, I'm not quite sure. Can we settle on 'uncommon'?

My point was that if you're creating a named nested class, put static on it unless you have a very good reason not to.

Winston
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

E Armitage wrote:

Tarun Oohri wrote:
1. Why it is that we can not define anything static inside the regular inner class (not talking about the static inner class).


We can define static variables but then they must be final.



To be exact, this exception to the rule, is an exception for compile time constant variables. So, the variable must be more than just final -- it also needs to be assigned to a constant expression, assigned at declaration, and of a type that can be a compile time constant. Otherwise, it will not be a compile time constant variable, and will not be allowed.

Henry
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic