• 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

"inner class"

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
Can I have my outer class access its inner (static/non-static) class variables??
If so what variables(whether private,public ,etc) can be accessed.
Can someone give me a syntax as to how to access the inner class variable from the outer class?
Any help is appreciated..
thanks,
 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
for an outer class to access its inner class variables, the inner class variables can be of public, private or protected access specifier.
but the variables cannot be static!! even declaring an inner class variable as static will result in compile time error.
rgds,
shashi
 
Shashi Kanta
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oops!! sorry! 4got to include the sample code!
here it is:

// hope that helps u.
rgds,
Shashi
 
manju kumar
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks shashi..
But my Q was can a outer class access the vars of inner class just like it does its own vars??..i mean without instantiating the inner class..say i have a separate class which has the main method..tell me how in such a situation??..hope u wud help me..
regards,
 
Ranch Hand
Posts: 1936
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you define Inner class as static, you can have Static variable in it! Of no good use though.
And, if inner class is static, outer class can definitely access inner class variable with out instantiating the inner class. Just think of inner class as a Structure in C, that will solve the issue. (ignore the last line if you don?t know C).
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ashok Manayangath:
If you define Inner class as static, you can have Static variable in it! Of no good use though.


Except for the fact that such a class is called a nested class, not an inner class... :roll:
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by manju kumar:

But my Q was can a outer class access the vars of inner class just like it does its own vars??..i mean without instantiating the inner class..


Member variables of an inner class are *instance variables* of that class. That is, without an instance of that class, there is no variable to refer to...
 
manju kumar
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
After trying with most of the combinations,i reached the foll conclusion :
For a normal inner class, the outer class cannot access its inner class vars whether static or not.
For a static inner class, the outer class can access its inner class vars if they are static and no if they are non-static.
(with condition that the inner class is not yet instantiated)
plz let me know if i am correct or even otherwise.
Thanks,
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by manju kumar:
For a normal inner class, the outer class cannot access its inner class vars whether static or not.
For a static inner class, the outer class can access its inner class vars if they are static and no if they are non-static.
(with condition that the inner class is not yet instantiated)


Yes. To reiterate, the reason for this is that (non-static) variables of inner/nested classes (and for any regular classes, too) are *not* bound to thaat class, but to an object of that class.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic