• 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
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Jaworski mock - statements on non-static inner class  RSS feed

 
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found this question in Jaworski's mock exam.
Which statement is true about a non-static inner class?
a. It must implement an interface.
b. It is accessible from any other class.
c. It can only be instantiated in the enclosing class.
d. It must be final if it is declared in a method scope.
e. It can access private instance variables in the enclosing object
I answered b&e. But the answer given is only e. Could somebody please explain what am i missing here.
Thanks.
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sree,
If b) was worded more precisely:
"It is DIRECTLY accessible to any other classes"
then you probably wouldn't have selected it.
May be this is what they meant. You probably assumed:
"It is accessible to any other classes, but provided this access is made thru an instance of the outside class".
See JavaRanch good tutorial on this.
JRoch
 
sreelakshmi sarma
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks JRoch.
 
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sree,
Even if you have access to the Outer class from Another class you may or may not have access to the inner class in the Another class. To put it simple, If the inner class is private to the outer class then you CANNOT access it from other classes. See the foll example. If I remove the comment a compiler error will occur. You know which error.
<pre>
class Outer {
private class PrivateInner {
}
class PackageInner {
}
void m1() {
PrivateInner privateInner = new PrivateInner();
PackageInner pacakgeInner = new PackageInner();
}
}
class AnotherClass {
void m2() {
Outer outer = new Outer();
//Outer.PrivateInner outerPrivate = outer.new PrivateInner();
Outer.PackageInner outerPaclkage = outer.new PackageInner();
}
}
class test {
static public void main(String[] args) {
}
}
</pre>
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Two more things:
A "non-static inner class" could also include a local class or an anonymous class (which is a type of local class). These are not accessible outside their scope. Probably Jaworski meant a non-static member class.
A member class will not be accessible in outside classes unless it has an appropriate access modifier. So B is not true in general unless the class is public.
I'm moving this to Mock Exam Errata. Thanks!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!