*
The moose likes Beginning Java and the fly likes Strange STATIC BLOCK behaviour 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 » Java » Beginning Java
Bookmark "Strange STATIC BLOCK behaviour" Watch "Strange STATIC BLOCK behaviour" New topic
Author

Strange STATIC BLOCK behaviour

Satish Kumar
Greenhorn

Joined: Feb 18, 2005
Posts: 23
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

Joined: Feb 18, 2005
Posts: 23
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 )
Sonny Gill
Ranch Hand

Joined: Feb 02, 2002
Posts: 1211

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.


The future is here. It's just not evenly distributed yet. - William Gibson
Consultant @ Xebia. Sonny Gill Tweets
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.


    [Jess in Action][AskingGoodQuestions]
    Satish Kumar
    Greenhorn

    Joined: Feb 18, 2005
    Posts: 23
    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

    Joined: Feb 18, 2005
    Posts: 23
    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

    Joined: Feb 18, 2005
    Posts: 23
    Hmm ! is this question silly or wat ?
    Ernest Friedman-Hill
    author and iconoclast
    Marshal

    Joined: Jul 08, 2003
    Posts: 24183
        
      34

    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

    Joined: Feb 18, 2005
    Posts: 23
    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
    Marshal

    Joined: Jul 08, 2003
    Posts: 24183
        
      34

    "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...
    Ilja Preuss
    author
    Sheriff

    Joined: Jul 11, 2001
    Posts: 14112
    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*!


    The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Strange STATIC BLOCK behaviour