This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread 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 Android Security Essentials Live Lessons this week in the Android 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: 9291
    
  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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: some confusion about inheritance contained static.
 
Similar Threads
Static Member Initialization
K&B chapter 2 question 11
Is Static veriable is visibile to subclass
private constructor
Are questions in real exam intentionally misleading too?