• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Instantiation of a static nested class

 
John Summers
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Nidhi Sar
Ranch Hand
Posts: 252
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
John Summers
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
static_inner.jpg
[Thumbnail for static_inner.jpg]
static nested class syntax
 
Ankit Garg
Sheriff
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 252
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic