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]
# 1
This is similar to using a static variable using an instanceinstead of the Class name. The instance type is used to identifythe enclising class name
PMJaina at 2007-7-14 22:09:16 > top of Java-index,Java Essentials,New To Java...
# 2
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.
Lokoa at 2007-7-14 22:09:16 > top of Java-index,Java Essentials,New To Java...
# 3

> 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?

kilyasa at 2007-7-14 22:09:16 > top of Java-index,Java Essentials,New To Java...
# 4

> 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

kilyasa at 2007-7-14 22:09:16 > top of Java-index,Java Essentials,New To Java...
# 5

> > 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.

pbrockway2a at 2007-7-14 22:09:17 > top of Java-index,Java Essentials,New To Java...
# 6

> that is not correct. Can you post some material to

> substantiate this description of inner classes?

I assume this was answered by the quote from the actual language specification saying exactly the same as what I said?

Now, can you post some material to illustrate why you were apparently so dang sure that it was incorrect? ;-)

Lokoa at 2007-7-14 22:09:17 > top of Java-index,Java Essentials,New To Java...