• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Are static Initializers (and static fields) inherited ?  RSS feed

 
Ranch Hand
Posts: 101
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi. There's an exercise question In head first java where you have to predict the output of a program. According to the answer , It seems like the static Initializer Is inherited from the parent class.
Is that the case ?




output:
%java StaticTests
super static block
static block 3
in main
super constructor
constructor
 
Ranch Hand
Posts: 43
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot do StaticSuper on line 05.
Anonymous code block cannot be named.
It can be eather static or regular (no static).

When you run the code:
1. Static anon block in StaticSuper (line 02) executed to be kept before main starts.
2. Static rand created(line 11).
3. Static anon block in StaticTests(12) executed to be kept before main starts.
4. main starts (line 19)
5. If you delete StaticSuper word at line 05, anon code block will be running for superclass (line 05).
6. constructor StaticTests() executed and new instance returned to main at line 21.
 
Nikolas Nikolaou
Ranch Hand
Posts: 101
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply.Line 5 should be StaticSuper().It was an Intentional error for the exercise.
If StaticSuper Is not Inherited , why does It run If the static main method Is In a different class ?
 
author
Posts: 23832
140
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Nikolas Nikolaou wrote:
If StaticSuper Is not Inherited , why does It run If the static main method Is In a different class ?



Instances of StaticTests classes IS-A instances of StaticSuper, so instantiating them will require constructors of the StaticSuper class to run as well. Not sure how you concluded that it is related to inheritence -- it's invocation is merely part of the initialization process, when the instance was created at line 21.

Henry
 
Nikolas Nikolaou
Ranch Hand
Posts: 101
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply.What I meant to say was why does the static Initializer of StaticSuper run before the main() method In the other class according to the output.
 
Henry Wong
author
Posts: 23832
140
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Nikolas Nikolaou wrote:Thanks for the reply.What I meant to say was why does the static Initializer of StaticSuper run before the main() method In the other class according to the output.



First, I never confirmed / checked where (or if) it is mentioned in the JLS -- so take this answer with a grain of salt.


It seems that before any static method is called, the class (along with its super classes) must be loaded first. And loading of the class (and superclass) files will trigger the execution of the static initializers. This is probably also true for instance methods, but those classes are loaded when the object is instantiated, so calling the method doesn't trigger any new class files to be loaded.

Henry
 
Nikolas Nikolaou
Ranch Hand
Posts: 101
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the Info. <salt>If that's the case</salt> It makes sense !
 
Master Rancher
Posts: 4076
47
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry pretty much has it correct.

On loading a class the JVM will ensure its parent class is loaded, and then prepared.
It's the Preparation stage that creates any static attributes, and runs any static initialisers.
So, in the example above, the JVM will load StaticTests, see it has a dependency on StaticSuper, so loads that and so the static code is executed...all before the main() is run.

Here's the JVM spec.
 
Dave Tolls
Master Rancher
Posts: 4076
47
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, and here's the JLS section covering class loading.
 
I can't take it! You are too smart for me! Here is the tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!