static inner class
Question :
Given the following class file , how can you get a reference to an object of type Inner ?
class Outer {
// some valid lines of code
static class Inner {
//some more valid lines of code
}
}
Options :
a . new Outer ( ) . new Inner ( )
b . Outer . new Inner ( )
c . new Outer . Inner ( )
d . Outer . Inner ( )
Answer is a and c.
I can understand c, but why a?
Since the inner class is static, it should not have outer class instance enclosing it, isint it?
[561 byte] By [
jaaya] at [2007-10-3 4:09:16]

> Question :
> Given the following class file , how can you get a
> reference to an object of type Inner ?
>
> class Outer {
>
> // some valid lines of code
>
> static class Inner {
> //some more valid lines of code
> }
> }
>
> Options :
>
> a . new Outer ( ) . new Inner ( )
> b . Outer . new Inner ( )
> c . new Outer . Inner ( )
> d . Outer . Inner ( )
>
> Answer is a and c.
> I can understand c, but why a?
> Since the inner class is static, it should not have
> outer class instance enclosing it, isint it?
new Outer().new Inner();
wouldnot even compile if Inner is a static inner class
> > Just a note on terminology: a class declared inside
> > another class is a nested class. Only a
> > non-static nested class is called an inner
> > class.
>
> that is not correct. Can you post some material to substantiate this
> description of inner classes?
From the JLS, http://java.sun.com/docs/books/jls/third_edition/html/classes.html
Chapter 8, Classes
"A nested class is any class whose declaration occurs within the body of
another class or interface. A top level class is a class that is not a nested class."
8.1.3 Inner Classes and Enclosing Instances
"An inner class is a nested class that is not explicitly or implicitly declared static"
This section includes further distinctions with examples and references:
"Inner classes include local (?4.3), anonymous (?5.9.5) and non-static
member classes (?.5)."
So Inner is a nested but not an inner class; a distinction that becomes important
when you use a class instance creation expression.
> new Outer().new Inner();
>
> wouldnot even compile if Inner is a static inner class
I get an error "Illegal enclosing instance specification for
type Outer.Inner". The same applies if I tryOuter o = new Outer();
o.new Inner();
According to the JLS http://java.sun.com/docs/books/jls/third_edition/html/expressions.html
15.9 Class Instance Creation Expressions "# Unqualified class instance creation
expressions begin with the keyword new. An unqualified class instance creation
expression may be used to create an instance of a class, regardless of whether
the class is a top-level (?.6), member (?.5, ?.5), local (?4.3) or anonymous
class (?5.9.5).
# Qualified class instance creation expressions begin with a Primary. A
qualified class instance creation expression enables the creation of instances
of inner member classes and their anonymous subclasses. "
Note that a class instance creation expression like "new Outer()" is itself
a primary.
Both "new Outer().new Inner()" and "o.new Inner()" are, therefore,
qualified class instance creation expressions and, as such, not
suitable for a nested (but non-inner) class like Inner.