aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Mock exam Q- Which variables can an inner class access? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Mock exam Q- Which variables can an inner class access? " Watch "Mock exam Q- Which variables can an inner class access? " New topic
Author

Mock exam Q- Which variables can an inner class access?

biju joseph
Greenhorn

Joined: Aug 02, 2002
Posts: 7
Hi,
Following is a question from JohnJunt Mock Exam.
Which variables can an inner class access from the class which encapsulates it?
A. All static variables
B. All final variables
C. All instance variables
D. Only final instance variables
E. Only final static variables
Select all correct answers

The answers are A, B,C. But i think this is wrong because:
1. methods of a static inner class cannot access instance variables or methods of the enclosing class. This is because static inner class doesnot have a reference to the enclosing class object.
2. methods of static local class cannot access instance variables of the enclosing class. same reason as above
3. if the anonymous inner class is static(implicitly), it cannot access any instance variables of the enclosing class
When exam says inner class, which inner class do they mean?. Is that all inner classes or a specific type of inner class?
Thanks
biju
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
This isn't a well-worded exam question. But an anonymous inner class is not implicitly static. And I don't think it's legal to declare a "local" static class (that is, inside a method).


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
I agree that it's not a well-worded exam question. However, you can declare a "local" static class. Declare it inside a static method and don't use a static modifier for this class. It will be implicitly static. The same goes for an anonymous class.
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Biju,
If you are looking for more questions on inner classes, then try this one.

Would anyone like to suggest an answer?


Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
bani kaali
Ranch Hand

Joined: May 05, 2002
Posts: 42
this code will give complier error at line 8.
static class StaticNested cannot access instance varible int c.
Top level nested classes does not have access to non-static members of the enclosing class.
-bani
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
bani,
Yes!
Your answer is correct.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Mock exam Q- Which variables can an inner class access?
 
Similar Threads
Inner class mult choice
Jaworski mock - statements on non-static inner class
A doubt on inner class
Interview questions
any difference between inner class and nested class?