• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Strange STATIC BLOCK behaviour

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yesterday i was experimenting with STATIC BLOCK and found this strange behaviour

, Can some 1 plzz explain me the reason behind this output

Here is the code


--------------------------
OUTPUT :

CLASS A loaded
1
--------------------------

My doubt here is
1 . Y are the static blocks of B,C not executed ?
 
Satish Kumar
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry , If the moderator is around can u plzz delete the other duplicate post. (The reason for that was wen i posted for 1st time , u r server reported internal error , so i posted again only to find the last 1 was posted )
 
Ranch Hand
Posts: 1211
Mac IntelliJ IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interesting indeed.

If I change the print statement to -


I get the output 'class C'.
This means that none of the static blocks were executed.

I would make a wild guess that it is some kind of optimization by the JVM, deferring full loading of a class to as late as needed. Which should be ok, unless it is directly in conflict with something in JLS.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From the Java Language Specification (section 12.4.1 of the Third Edition


Initialization of a class consists of executing its static initializers and the initializers for static fields declared in the class. Initialization of an interface consists of executing the initializers for fields declared in the interface.
...
A class or interface type T will be initialized immediately before the first occurrence of any one of the following:

  • T is a class and an instance of T is created.
  • T is a class and a static method declared by T is invoked.
  • A static field declared by T is assigned.
  • A static field declared by T is used and the field is not a constant variable (�4.12.4).
  • T is a top-level class, and an assert statement (�14.10) lexically nested within T is executed.


  • Invocation of certain reflective methods in class Class and in package java.lang.reflect also causes class or interface initialization. A class or interface will not be initialized under any other circumstance.



    Because the static member "x" is declared in A and not in C, only A and D need to be (or will) be initialized in Satish's example -- even though A, B, C, and D are all loaded.

    Likewise, in Sonny's example, none of these conditions are met for C, so C isn't initialized, and therefore neither are any of its parents. Actually using the C.class object for something may trigger initialization.
     
    Satish Kumar
    Greenhorn
    Posts: 23
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    hmm , i guess in addition to rules specified above by Ernest Friedman-Hill , a CLASS also gets initialized(i mean STATIC block getting extecuted) when it's sub class is called. Here is small modification of my code , which proves my point :


    -------------------------
    OUTPUT
    CLASS A loaded
    CLASS B loaded
    1
    -------------------------
     
    Satish Kumar
    Greenhorn
    Posts: 23
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    And what makes this static behaviour much more intresting and difficult to interpret is when you have two classes containing static int x

    Here is the CODE :


    ----------------------------
    output :
    CLASS A loaded
    CLASS B loaded
    3
    -------------------------------
    So the JVM just doesnt stop when it finds static variable x in CLASS A instead searches it's subclasses .
     
    Satish Kumar
    Greenhorn
    Posts: 23
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hmm ! is this question silly or wat ?
     
    Ernest Friedman-Hill
    author and iconoclast
    Posts: 24207
    46
    Mac OS X Eclipse IDE Chrome
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    When a class is initialized, its superclass always initialized, too. The same section of the JLS makes this clear if you read the whole thing.

    As far as the "not stopping when it finds A.x," that's not it: the compiler knows at compile time that it wants B.x. Before B.x can be read, B must be initialized; and when B is initialized, A is initialized.

    This isn't really as confusing as you're making it out to be.
     
    Satish Kumar
    Greenhorn
    Posts: 23
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks Ernest for you'r replies, but 1 point in JLS(specified below) is unclear to me .



    Can u plzz elaborate on this(especially that assert statement)
     
    Ernest Friedman-Hill
    author and iconoclast
    Posts: 24207
    46
    Mac OS X Eclipse IDE Chrome
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    "Lexically nested within T" just means inside the braces for class T:



    Anything that can go where XXX is, including inner classes, etc, is "lexically nested within T."

    But I can't actually think of a case where this is separate from all of the other cases -- this language is new in the third edition, and I've never thought about it before! Maybe somebody else knows more...
     
    author
    Posts: 14112
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by Ernest Friedman-Hill:
    As far as the "not stopping when it finds A.x," that's not it: the compiler knows at compile time that it wants B.x.



    With other words, the *byte code* that the compiler produces and gets executed by the VM doesn't contain a reference to C.x in the first example, but to A.x!

    You always have to remember that fields *are not polymorphic*!
     
    Let's get him boys! We'll make him read this tiny ad!
    a bit of art, as a gift, that will fit in a stocking
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic