• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

some confusion about inheritance contained static.

 
kevin saber
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



The result is "alpha subsub", not the "subsub" i would have thought.

is it right to say, in a tree of inheritance, all the classes share one static variable?
 
Ankit Garg
Sheriff
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A static variable has only one copy in one JVM. So even inheritance doesn't matter in this case...
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Compiler interpret as


i.e,
Here Compiler add super() as a first statement in Constructor which calls your Super class constructor

kevin wrote: is it right to say, in a tree of inheritance, all the classes share one static variable?


yes, if it is not a private and subclass dont have the duplicate

hth
 
kevin saber
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh~~~ thanks you two!!!
But... I still have something not to understand. please look at this example as follow:


now that all classes in the tree of inheritance share one static variable, the result should be "parent", but it isn't....why???
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please read the last statement of my previous post
 
Brij Garg
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

now that all classes in the tree of inheritance share one static variable, the result should be "parent", but it isn't....why???

Becasue child class has its own copy of static variable.
If child class does not have its own copy then it will share the copy from the parent class.

"Seetharaman Venkatasamy" has clearly pointed this out in his post.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic