aspose file tools*
The moose likes Beginning Java and the fly likes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark ""inner class"" Watch ""inner class"" New topic
Author

"inner class"

manju kumar
Greenhorn

Joined: May 29, 2002
Posts: 5
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,
Shashi Kanta
Ranch Hand

Joined: May 08, 2002
Posts: 89
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

Joined: May 08, 2002
Posts: 89
oops!! sorry! 4got to include the sample code!
here it is:

// hope that helps u.
rgds,
Shashi
manju kumar
Greenhorn

Joined: May 29, 2002
Posts: 5
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,
Ashok Mash
Ranch Hand

Joined: Oct 13, 2000
Posts: 1936
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).


[ flickr ]
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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:


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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

Joined: May 29, 2002
Posts: 5
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
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: "inner class"