aspose file tools*
The moose likes Java in General and the fly likes Is Static veriable is visibile to subclass Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Is Static veriable is visibile to subclass" Watch "Is Static veriable is visibile to subclass" New topic
Author

Is Static veriable is visibile to subclass

himkar dwivedi
Greenhorn

Joined: Mar 02, 2010
Posts: 5
Hi,
3. class Alpha {
4. static String s = " ";
5. protected Alpha() { s += "alpha "; }
6. }
7. class SubAlpha extends Alpha {
8. private SubAlpha() { s += "sub "; }
9. }
10. public class SubSubAlpha extends Alpha {
11. private SubSubAlpha() { s += "subsub "; }
12. public static void main(String[] args) {
13. new SubSubAlpha();
14. System.out.println(s);
15. }
16. }

On line no 8 and 11, i am not able to understand that how s is visible.
sandeeprajsingh tandon
Ranch Hand

Joined: Mar 06, 2009
Posts: 71
Sorry,
I Did not get your question. What makes you think s should not be visible?

Of course, s is a static variable which means there is only copy of it where ever it is.
but you reckon there is any rule on its availability in subclasses.
himkar dwivedi
Greenhorn

Joined: Mar 02, 2010
Posts: 5
I meen to say that here should be like
Alpha.s or something by reference.
Matt Cartwright
Ranch Hand

Joined: Aug 25, 2008
Posts: 149

it's not the static that determines visibility, it's the access level modifier (e.g. private).

In your (badly formatted) example you are not specifying any visibility.
So your static variable is visible to Class & Package.
That's the default.

You don't specify any package either. So all classes reside
in the same (default) package.

That's why it is visible.

Hope that helps
Matt

Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Matt Cartwright wrote:In your (badly formatted) example you are not specifying any visibility.


Yes. please use *code tag* to post your code
Matt Cartwright
Ranch Hand

Joined: Aug 25, 2008
Posts: 149

himkar dwivedi wrote:I meen to say that here should be like
Alpha.s or something by reference.


yep, that's a good thing to do
makes the warnings go away

but hasn't anything to do with visibility.

It could be SubAlpha.s or Alpha.s where I personally
would prefer Alpha.s, so you know where it has been defined.

M
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is Static veriable is visibile to subclass