Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

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

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Biju,
If you are looking for more questions on inner classes, then try this one.

Would anyone like to suggest an answer?
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bani,
Yes!
Your answer is correct.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic