• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Inner

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which statement is true about a non-static inner class ?
1) It must implemet an interface.//false
2) It is accessible from any other class.//false...private class can't be accessed by other class
3) It can only be instantiated in the enclosing class.//false
4) It must be final if it is declared in a method scope.//false.....the class in a method scope can access only final members.
5) It can access private instance variables in the enclosing object.//true

I don't remember from which mock test I noted this question and hence no answer for sure.
 
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Umesh,
Your answers are correct. It is from Jargon Mock Exam.
Your reason for 4th answer is not exactly relating to the context here. What you said is correct. Also note that a class inside a block (method/stactic blocks/instance blocks) CAN be defined as a final class. Nothing restricts that. The sentence 'must be final' makes the answer wrong.
regds
maha anna
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

3) It can only be instantiated in the enclosing class.//false
true - a non static inner class MUST have an instance of the enclosing class!
 
maha anna
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java Nut,
Please read the answer once more.
3) It can only be instantiated in the enclosing class.
The word 'in' makes the answer false. If it would have been the word It can only be instantiated using the enclosing class. then your comment is ok.
An non-static inner class (not local method classes/anonymous classes) CAN BE instantiated in any other outsideer classes provided the outsider class has access permission to both the outer class and the non-static inner class.
<pre>
class Outer {
class NonstaticInner {}
}
class Outsider {
public void m1() {
Outer outerInstance = new Outer();
Outer.NonstaticInner outerinner = outerInstance.new NonStaticInner();
}
}

</pre>
regds
maha anna
 
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please read the following question again and tell me whether my thinking is correct or not.

Which statement is true about a non-static inner class ?
1) It must implemet an interface.//false
2) It is accessible from any other class.//false...private class can't be
accessed by other class
3) It can only be instantiated in the enclosing class.//false
4) It must be final if it is declared in a method scope.//false.....the class in a
method scope can access only final members.
5) It can access private instance variables in the enclosing object.//true

How can that third statement be false. When we are saying that option 2 is false because inner class could be private class. Then it should be the same case with option 3 also right??? What i am trying to say here is how can anybody instantiate an inner class if it's private.
Then we should say.
3) It can only be instantiated in the enclosing class.//true because it could be private inner class right???
Please correct me if am wrong.
Thanks.
 
maha anna
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sree,
In my humble opinion, When you analyze a set of answers try to analyze with all possible cases. We have to put all the poosible inputs for a rugged test right?. We should not take just one possiblity and apply only that input for the test. Having said that,
In our context, what are the possible access levels applicable for an inner class? It can be public/private/protected right? And in particular this qstn does not explicitly specify any info about the access level of the inner class. We have bear that also in our mind. Now we have to apply the test from answer 1 to 4 for private innerclass, package level inner class, protected inner class , public inner class, method-level local inner class, anonymous inner class Now you can see for yourself that all the answers except the last answer fail. Do you get the idea? We have to check whether a particular statement (answer) can be generalized like that. This is the proper way of testing not only for this small qstn. (Even in our personal life also I think ). Please reply what do think of the qstn and the answers.
regds
maha anna

[This message has been edited by maha anna (edited April 19, 2000).]
 
sreelakshmi sarma
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This question is driving me nuts Maha. But what you said is correct. I agree with you.
Thanks.
 
maha anna
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sree,See carefully.
1)It must implemet an interface.
false. The 'must' makes the statement false. We can have a simple inner class like the foll.
<pre>

Example:
class Outer {
class Inner { //need not implement an interface
}
}

</pre>
2) It is accessible from any other class.
false. We can't generalize like this. The 'is accessible' makes the statement false. For example we can have a private inner class like the foll.
<pre>

Example:
class Outer {
private class Inner { //can't be seen outside
}
}

</pre>

3) It can only be instantiated in the enclosing class.
false. The 'only be instantiated in the enclosing ' all together makes the statement false. We can have inner class like the foll. and still can be instantiated outside of the class where it is defined.
<pre>

Example:
class Outer {
class Inner { }
}
class Test { //outsider class can make an instance of above inner
void m1() {
Outer.Inner outIn = new Outer().new Inner();
}
}

</pre>
4) It must be final if it is declared in a method scope.
false. need not be. we can have a local inner class like the foll.
<pre>

Example:
class Outer {
void m1() {
class LocalInner { //need not be final
}

}
}

</pre>
5) It can access private instance variables in the enclosing object.
true.The proof follows . At last one true statement
<pre>

Example:
class Outer {
private int privateVar=10;
class Inner {
Inner() {
System.out.println("See. I got your private data :"+Outer.this.privateVar);
}
}
}

</pre>
regds
maha anna

[This message has been edited by maha anna (edited April 19, 2000).]
 
sreelakshmi sarma
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Maha.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic