What is true about inner class? Select one 1 an Inner class can access all variables in the any enclosing scope 2 an Inner class can access all variables in the enclosing scope 3 an inner class can be declared as private 4 an Anonymous inner class can be extended from class and can implement an interface ANS 3 BUT WHY 2 IS WROUNG�� AND SEE IT What a static inner class can access select one A. Any variables in the enclosing scope B. Final variables in the enclosing scope C. Static variables declared in side the method D. Static variables declared in side the outer class Ans
Valentin Crettaz
Gold Digger
Sheriff
Joined: Aug 26, 2001
Posts: 7610
posted
0
1 is wrong since classes local to methods may only access final variables in the method scope 2 same explanation as 1 3 is naturally correct 4 is wrong. An anonymous inner class can either extend a class OR implements an interface (in which case it extends Object implicitely) You may find it interesting to read the following link http://developer.java.sun.com/developer/Books/certification/certbook.pdf about inner classes. HIH ------------------ Valentin Crettaz Sun Certified Programmer for Java 2 Platform
I got it I have wroung on variables and members Whice diffrence of them?
Uma Viswanathan
Ranch Hand
Joined: Jun 14, 2001
Posts: 126
posted
0
Valentin: I accept that option 1 is wrong bcoz local inner class can not access all the variables in the enclosing scope but... option 3 is also wrong for the similar reason...bcoz a local inner class can not be declared as private. We can not apply any access modifier to the class inside the method... If they strictly speak inner class as class inside the class (but not inside the method), then we have two cases viz. static and non-static. option 1 is wrong bcoz the static inner class cannot access the non-static members of the enclosing class. option 3 is correct bcoz both static and non-static inner classes can be marked as private... Could someone confirm what do they mean by inner class? Is it strictly a class inside another class and not inside a method?... Uma
Valentin Crettaz
Gold Digger
Sheriff
Joined: Aug 26, 2001
Posts: 7610
posted
0
No you're right Uma... the inner class subject has always been a hot topic since the distinction between all types of inner classes is not trivial at the first sight. Then option 3 should be wrong too since classes local to methods cannot be declared private. Again read the link I gave above which explains quite in details what are inner classes. Also you may want to read the Inner class Specification HIH ------------------ Valentin Crettaz Sun Certified Programmer for Java 2 Platform
Hi val when I go to link: http://developer.java.sun.com/developer/Books/certification/ to get other pdf material. but I get a simple page which has chapter 6 of RHE. If you would have not given the link then how am i suppose to find that pdf from above link?? I was looking for some link on above said URL for the pdf or I put my query in another way: How should one come to know abt this pdf as there is no link given to it.
------------------ Regards Ravish
"Thanks to Indian media who has over the period of time swiped out intellectual taste from mass Indian population." - Chetan Parekh
amit mawkin
Ranch Hand
Joined: Oct 31, 2001
Posts: 73
posted
0
Hi I believe following facts are right 1.An Inner class can access any member variable no matter what the modifier is , of the outer enclosing class. 2.An Inner class not inside a method can extend as well as implement other classes and interface 3.An Inner class inside a method cannot have any modifier accept the default. 4.An Inner class inside a method cannot have any static members 5.An Inner class declared inside a method can only access members declared final of the enclosing method and any member with any modifier of the outer enclosing class. correct me if am wrong please Even I am learning fundas of inner classes 6. ------------------ coffee drinker
Ravish, Here is how I found that link... I went to http://java.sun.com and in the search text field (upper right corner) I typed "inner classes". Then I got the search result page and the first link was the one I posted... That's how... HIH ------------------ Valentin Crettaz Sun Certified Programmer for Java 2 Platform
amit mawkin
Ranch Hand
Joined: Oct 31, 2001
Posts: 73
posted
0
Hi are my assumptions correct or am I totally wrong that nbody commented on that Val where r u? ------------------ coffee drinker
Valentin Crettaz
Gold Digger
Sheriff
Joined: Aug 26, 2001
Posts: 7610
posted
0
Amit, Look at the first link I gave on this page and go to page 225 of the linked PDF document. There are the answers to your questions. 1.An Inner class can access any member variable no matter what the modifier is , of the outer enclosing class. Wrong (see table 7.1 on the link (column 5)) 2.An Inner class not inside a method can extend as well as implement other classes and interface true but classes inside methods can do that too 3.An Inner class inside a method cannot have any modifier accept the default. correct 4.An Inner class inside a method cannot have any static members correct, even local classes in static methods !!! 5.An Inner class declared inside a method can only access members declared final of the enclosing method and any member with any modifier of the outer enclosing class. Two cases: inner classes in static methods and non-static methods. The former can access all members of enclosing context + final local constants. The latter can only access static members of enclosing context + final local constants. HIH ------------------ Valentin Crettaz Sun Certified Programmer for Java 2 Platform
Uma Viswanathan
Ranch Hand
Joined: Jun 14, 2001
Posts: 126
posted
0
Valentin: 4.An Inner class inside a method cannot have any static members correct, even local classes in static methods !!! "An inner class can have static members if we are also applying final modifier." Check this code... public class Test { void display() { class Inner{ final static int x = 0; Inner(){System.out.println("x = " + x);} } Inner i = new Inner(); } public static void main( String[] args ) { Test t = new Test(); t.display(); } } Do provide your views Valentin... Uma
Valentin Crettaz
Gold Digger
Sheriff
Joined: Aug 26, 2001
Posts: 7610
posted
0
Right, I forgot that silly one Sorry! ------------------ Valentin Crettaz Sun Certified Programmer for Java 2 Platform