This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes some confusion about inheritance contained static. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "some confusion about inheritance contained static." Watch "some confusion about inheritance contained static." New topic
Author

some confusion about inheritance contained static.

kevin saber
Greenhorn

Joined: Mar 07, 2010
Posts: 21



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

Joined: Aug 03, 2008
Posts: 9293
    
  17

A static variable has only one copy in one JVM. So even inheritance doesn't matter in this case...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575



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

Joined: Mar 07, 2010
Posts: 21
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

Joined: Jan 28, 2008
Posts: 5575

Please read the last statement of my previous post
Brij Garg
Ranch Hand

Joined: Apr 29, 2008
Posts: 234

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.
 
Don't get me started about those stupid light bulbs.
 
subject: some confusion about inheritance contained static.