This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
i just played a Rundup game and #232 make me at a loss a non-static inner class always has free access to all member data of its enclosing class? the answer is TRUE.but i think it should be FALSE my reasion is that local class belongs to inner class and it has access only to variable of enclosing class declared final i need a clarification, thanks in advance
hi as far as my knowledge goes, a non static inner class declared in a class can have access to all member variables of its enclosing class. but an inner class defined in a METHOD can have access to only final variables of the method , and this inner class inside a method can freely access outer class variables , but can access only final variables of the method inside which it is defined.
rule is inner class inside a class ::: free access to all member variables of the class.
inner class inside a method: access to only final variables of the method anyone pls correct me if i am wrong. regards Kamal J
Hi, Can I try ? From what I understand, a non-static inner class defined within another class has access to all variables declared in the enclosing class. In another case, if the inner class in created within the scope of a method of the outer class, then the inner class can access the variables declared in that method also, ONLY IF those variables are final. Hope that helps. Please correct me if i am wrong. (fyi, there is a lot of info on inner classes in maha annas forum.)
-----------------------------------------------------<BR> Koray GUCLU ~ the sky has no limit ~<BR> (B.s. Computer Engineer)<BR> Frankfurt am Main, Germany<BR> web : <A HREF="http://www.geocities.com/korayguclu/<BR rel="nofollow">" TARGET=_blank>http://www.geocities.com/korayguclu/<BR></A> mail: email@example.com<BR>-----------------------------------------------------
Let me try.... Classes can only access variables defined in that class or a superclass (depending on the access modifier). A non-static inner class can access all member variables. *it follows the rule above* If it is inside a method, you can access all members + finals within the method. *finals, because they get evaluated before the class compiles * If it is static, you can't access all member variables. Only static. * because static doesen't know about instance variables* That's what I think. Francisco.
Joined: Mar 23, 2001
Hi, all, thanks for your replies. I think i got the rule but i'm still wondering why the rule,i mean why a inner class defined in a method can have access to the local variables and why the "final" is nessesary for that access? Could anyone give a deep explanation? ------------------
Joined: Mar 22, 2001
Hi James, Heres my understanding When you declare a class within a method, it will be instantiated when the method is called. The local variables of the method go out of scope once the method is completed, but the class can still 'outlive' the method. If the class had access to the local variables, then we would have a conflict, right ? If the variable is final in the method, the class can access it since it will behave as a constant value. So no problem here. Please corret me if i am wrong here. Sajida
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com