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.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes regarding non-static inner class 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 "regarding non-static inner class" Watch "regarding non-static inner class" New topic

regarding non-static inner class

James Du
Ranch Hand

Joined: Mar 23, 2001
Posts: 186
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
kamal jaisingh
Ranch Hand

Joined: Feb 23, 2001
Posts: 39
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.
Kamal J
sajida kal
Ranch Hand

Joined: Mar 22, 2001
Posts: 89
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.)
koray guclu
Ranch Hand

Joined: Feb 23, 2001
Posts: 59
you can find information about inner classes from
It is a word formated document. You can read it offline.
Please write your comment to me.

Koray G��l�
(B.s. Computer Engineer)

-----------------------------------------------------<BR> Koray GUCLU ~ the sky has no limit ~<BR> (B.s. Computer Engineer)<BR> Frankfurt am Main, Germany<BR> web : <A HREF="<BR rel="nofollow">" TARGET=_blank><BR></A> mail:<BR>-----------------------------------------------------
Francisco I
Ranch Hand

Joined: Mar 27, 2001
Posts: 44
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.
James Du
Ranch Hand

Joined: Mar 23, 2001
Posts: 186
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?
sajida kal
Ranch Hand

Joined: Mar 22, 2001
Posts: 89
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.
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link:
subject: regarding non-static inner class
It's not a secret anymore!