Win a copy of Head First Android this week in the Android forum!
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Are static Initializers (and static fields) inherited ?

 
Ranch Hand
Posts: 101
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 23909
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 23909
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the Info. <salt>If that's the case</salt> It makes sense !
 
Rancher
Posts: 4801
50
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Rancher
Posts: 4801
50
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh, and here's the JLS section covering class loading.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic