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.


class Outer {
class NonstaticInner {}
}
class Outsider {
public void m1() {
Outer outerInstance = new Outer();
Outer.NonstaticInner outerinner = outerInstance.new NonStaticInner();
}
}

regds
maha anna

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 ?
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
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
maha anna

[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.


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

}
}


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.



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

}
}



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.



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();
}

}


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.



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

}
}

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



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

}
}


regds
maha anna

[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.

|