• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

regarding non-static inner class

 
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.)
 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
you can find information about inner classes from
http://www.geocities.com/korayguclu/
It is a word formated document. You can read it offline.
Please write your comment to me.

------------------
------------------------
Koray G��l�
(B.s. Computer Engineer)
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
James Du
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic