Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Non-static Inner Class

 
Kaz Yosh
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Kaz Yosh
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And here's some test code.
 
Yuan Ye
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can Non-static inner classes have static members?
 
Saket Barve
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can Non-static inner classes have static members?

No, a non-static inner class cannot have static members.
 
Brian Joseph
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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;
???
 
Yi Meng
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.....
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic