File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is Static veriable is visibile to subclass

 
himkar dwivedi
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 79
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I meen to say that here should be like
Alpha.s or something by reference.
 
Matt Cartwright
Ranch Hand
Posts: 152
Linux VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 152
Linux VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic