File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Instantiation of a static nested class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Instantiation of a static nested class" Watch "Instantiation of a static nested class" New topic
Author

Instantiation of a static nested class

John Summers
Ranch Hand

Joined: Oct 06, 2003
Posts: 125
Hi guys,

I have a quick syntax question about static nested inner classes. It's regarding K&B SCJP 6 pp.692.

Basically the question says:

"Which are true about a static nested class?" and option D is "If the outer class is named MyOuter, and the nested class is named MyInner, it can be instantiated using new MyOuter.MyInner();"

I just want to check this is a trick question. First off even though option D says "the nested class" they already said "true about a static nested class". Bearing this in mind then my understanding is that yes, this is correct, it CAN be instantiated this way, but in fact you don't have to put the "MyOuter" bit on for compilation to succeed.

I have this in one java file:


public class Outer {
static class Inner{}
}


And this in the main method in another class file:


Inner i2 = new Outer.Inner();//the longhand way, like the exam answer
Inner i = new Inner();//the shorthand way. this compiles fine!!



And it all compiles fine. So you don't HAVE to use new MyOuter.MyInner();

John
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

And this in the main method in another class file:

Inner i2 = new Outer.Inner();//the longhand way, like the exam answer
Inner i = new Inner();//the shorthand way. this compiles fine!!


Can you show us the full code of that class, have you used a static import??

First off even though option D says "the nested class" they already said "true about a static nested class".

As far as I remember, static inner classes are called nested classes and non-static inner classes are called inner classes...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Nidhi Sar
Ranch Hand

Joined: Oct 19, 2009
Posts: 252

John Summers wrote:
public class Outer {
static class Inner{}
}


And this in the main method in another class file:

Inner i2 = new Outer.Inner();//the longhand way, like the exam answer
Inner i = new Inner();//the shorthand way. this compiles fine!!




John,

Are you sure that you are puuting this block of code in the main method of another class file? It is not the main method of Outer?

Take a look at the following code. This is the full code of a file called Outer.java, the lines you say compile fine, will only compile fine (line numbers 1 & 2) if put in the outer class. They give a compile error(line numbers 3 & 4) if put in another class.



Import static will make 3 & 4 work too, but that's probably not what's going on here.

- Nidhi


"A problem well stated is a problem half solved.” - Charles F. Kettering
SCJP 6, OCPJWCD
John Summers
Ranch Hand

Joined: Oct 06, 2003
Posts: 125
Guys,

Thanks for the replies. Nidhi.. I think you are wrong. The whole point of a static inner class is that it is a top level class, it just happens to be defined within another classes definition. You do NOT have to use the Outer.Inner syntax. Please check the attached screenshot. You DO have to use this syntax for an inner class (i.e. one without the 'static' modifier).

However, if you are perverse you can, if you wish, use the Outer.Inner syntax to instantiate a static nested class. It is unneccessary but you can use it. Which brings me onto the whole point of my original post i.e. the original K+B question was a trick question.

John



[Thumbnail for static_inner.jpg]

Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

The whole point of a static inner class is that it is a top level class, it just happens to be defined within another classes definition. You do NOT have to use the Outer.Inner syntax.

This is not true. nested class is not a top level class. You HAVE to use the Outer.Inner syntax. Look at the import statement in your code, it was added by your IDE that's why you didn't notice it...
Nidhi Sar
Ranch Hand

Joined: Oct 19, 2009
Posts: 252

John Summers wrote:The whole point of a static inner class is that it is a top level class, it just happens to be defined within another classes definition.


It is not just a top-level class. It is like a top-level class SCOPED WITHIN another class. That effects it's name-space resolution.

John Summers wrote:
You do NOT have to use the Outer.Inner syntax.


Unless you use static imports [great catch, Ankit ], actually it will be mandatory to use Outer.Inner syntax from another class.

Good luck,
Nidhi
John Summers
Ranch Hand

Joined: Oct 06, 2003
Posts: 125
I see the import statement but this looks like a plain old import statement to me. Don't static imports have the 'static' keyword?
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

John Summers wrote:I see the import statement but this looks like a plain old import statement to me. Don't static imports have the 'static' keyword?

Yes its not a static import, but its because of that statement that you are able to avoid the syntax Outer.Inner. You can import static and non-static inner classes with an import statement like this...
John Summers
Ranch Hand

Joined: Oct 06, 2003
Posts: 125
right...

so this is nothing to do with the "static imports" feature. What I've done is just a plain old import..

So in summary if you do an import

import com.javaranch.Outer.Inner;

then you CAN use the shorthand syntax of Inner i = new Inner(); and you don't have to use Outer.Inner i = new Outer.Inner();

So ultimately any question which says you have to use Outer.Inner is wrong. You don't! You can import as above and use the shorthand syntax.

Which means my original first post was correct... you can use the longhand if you want to but if you import you can use the shorthand.

Thanks
john
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Instantiation of a static nested class
 
Similar Threads
Static Nested Classes
(Inner Classes) MyOuter.MyInner vs MyInner
I get a runtime error when using static Inner Class -- pls explain.
Static nested class question
A mistake in K&B book?