Author | Topic: Inner |
Umesh ranch hand |
posted April 13, 2000 09:19 AM
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 bartender |
posted April 13, 2000 10:38 AM
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
|
Java Nut unregistered |
posted April 13, 2000 10:41 AM
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 bartender |
posted April 13, 2000 11:19 AM
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.
regds
|
sree ranch hand |
posted April 19, 2000 08:56 AM
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 ?
Then we should say. Please correct me if am wrong. Thanks.
|
maha anna bartender |
posted April 19, 2000 09:55 AM
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 [This message has been edited by maha anna (edited April 19, 2000).]
|
sree ranch hand |
posted April 19, 2000 01:05 PM
This question is driving me nuts Maha. But what you said is correct. I agree with you. Thanks.
|
maha anna bartender |
posted April 19, 2000 01:55 PM
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.
2) It is accessible from any other class.
4) It must be final if it is declared in a method scope.
5) It can access private instance variables in the enclosing object.
regds [This message has been edited by maha anna (edited April 19, 2000).]
|
sree ranch hand |
posted April 19, 2000 02:07 PM
Thanks a lot Maha.
|
| | |