aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Inner Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Inner" Watch "Inner" New topic
Author

Inner

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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.
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
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

Joined: Nov 22, 2008
Posts: 18944

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

Joined: Jan 31, 2000
Posts: 1467
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
sreelakshmi sarma
Ranch Hand

Joined: Mar 02, 2000
Posts: 130
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

Joined: Jan 31, 2000
Posts: 1467
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

Joined: Mar 02, 2000
Posts: 130
This question is driving me nuts Maha. But what you said is correct. I agree with you.
Thanks.
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
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

Joined: Mar 02, 2000
Posts: 130
Thanks a lot Maha.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inner
 
Similar Threads
Jaworski mock - statements on non-static inner class
Inner
Interview questions
Innerclass Qn
problem from sun sample