This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Non-static Inner Class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Non-static Inner Class" Watch "Non-static Inner Class" New topic
Author

Non-static Inner Class

Kaz Yosh
Ranch Hand

Joined: May 22, 2003
Posts: 63
I have a question regarding nested class.
Below is from one of the Review Questions from "A programmers guide to Java" Chapter 7 7.7 Page 248.
Which of these statements are true?
Select all valid answers
(a) You cannot declare static members within a non-static inner class.
(b) If a non-static inner class is nested within a class named Outer, then methods within the non-static inner class must use the prefix Outer.this to access the members of the class Outer.
(c) All member variables in any nested class must be declared final.
(d) Anonymous classes cannot have constructors.
(e) If objRef is an instance of any nested class within the class Outer, then (objRef instanceof outer) would yield true.
Why is (b) not true?


"If anything can go wrong, it will"
Kaz Yosh
Ranch Hand

Joined: May 22, 2003
Posts: 63
And here's some test code.
Yuan Ye
Ranch Hand

Joined: Mar 05, 2003
Posts: 172
I think if the method of the outer class is a static method. The you should use Outer.method instead of using Outer.this.method. So b is not correct.
Kaz Yosh
Ranch Hand

Joined: May 22, 2003
Posts: 63
Thanks for the reply Yuan ye
but I still dont understand why b is wrong.
I use Toplevel.this.one from the method within nestedLevel class and it works.
Yuan Ye
Ranch Hand

Joined: Mar 05, 2003
Posts: 172
Yes, in that case since your varible is non-static it works. But what if the varible is a static one? Answer b said "must", I think that's why. You can do that, but not in any case.
Kaz Yosh
Ranch Hand

Joined: May 22, 2003
Posts: 63
can Non-static inner classes have static members?
Saket Barve
Ranch Hand

Joined: Dec 19, 2002
Posts: 229
can Non-static inner classes have static members?

No, a non-static inner class cannot have static members.
Brian Joseph
Ranch Hand

Joined: May 16, 2003
Posts: 160
A non-static inner class can not have static members. But it helps to know WHY, otherwise if you're like me, you'll likely forget.
I'm still in training for the SCJP myself, so correct me if I'm wrong, but I believe the reason is that since the inner class is non-static, the inner class does not exist unless there is an instance of the outer class first.
So you can't just do:
Outer.Inner.staticVar = 123;
Now, think about static inner classes, or as they call them "top-level nested inner classes". Why else would we be able to use static? We obviously can't use static for regular top-level classes, but we can for inner classes. Here you put the keyword "static" as part of the inner class delcaration. Now you are allowed to declare static members in the inner class because the inner class is static and has existence on it's own without the need for an instance of the outer class to exist, sort of like static member variables.

Hope this helped more than it confused. If you can grasp certain concepts, then in some cases like this you can figure it out without memorization.
Dan Lajoy
Greenhorn

Joined: May 28, 2003
Posts: 19
I was doing some studying today, and I thought in one of Dan Chisolm's mock exams he said you could have static members in non-static inner classes as long as they were compile time constants - like: static final int x = 2;
???


SCJP 1.4
Yi Meng
Ranch Hand

Joined: May 07, 2003
Posts: 270
I was doing some studying today, and I thought in one of Dan Chisolm's mock exams he said you could have static members in non-static inner classes as long as they were compile time constants - like: static final int x = 2;

i think in this example it is valid because that static is used together with final, which makes x to be a constant.
if you try put something like "static int x = 2;" in a non-static inner class, you are bonded to have a compiler error.....


Meng Yi
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
The reason b is wrong is because of the word "must". In your example you had to use it becuse you had a variable called one in the inner class. If you didn't then there would have been no need to use the Outer.this form.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
 
 
subject: Non-static Inner Class