Value of static variable not changed even after initializing the child class in Java [duplicate]











up vote
28
down vote

favorite
4













This question already has an answer here:




  • Execution of Java static blocks in subclasses

    2 answers




When I invoke the static variable y by using Checks.y (Checks being a subclass), the static block is not executed and the value of y doesn't get updated.



class Par {
static int y = 4;
}

class Checks extends Par {
static {
y = 5;
}
}

public class Check {
public static void main(String args) {
System.out.println(Checks.y); // here printing 4
}
}


As static is shared among all subclasses, the value is supposed to be updated.



What could be the reason behind it?










share|improve this question















marked as duplicate by Oleksandr, Yogu, Krease, Mark Rotteveel java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
2 days ago


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















    up vote
    28
    down vote

    favorite
    4













    This question already has an answer here:




    • Execution of Java static blocks in subclasses

      2 answers




    When I invoke the static variable y by using Checks.y (Checks being a subclass), the static block is not executed and the value of y doesn't get updated.



    class Par {
    static int y = 4;
    }

    class Checks extends Par {
    static {
    y = 5;
    }
    }

    public class Check {
    public static void main(String args) {
    System.out.println(Checks.y); // here printing 4
    }
    }


    As static is shared among all subclasses, the value is supposed to be updated.



    What could be the reason behind it?










    share|improve this question















    marked as duplicate by Oleksandr, Yogu, Krease, Mark Rotteveel java
    Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

    StackExchange.ready(function() {
    if (StackExchange.options.isMobile) return;

    $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
    var $hover = $(this).addClass('hover-bound'),
    $msg = $hover.siblings('.dupe-hammer-message');

    $hover.hover(
    function() {
    $hover.showInfoMessage('', {
    messageElement: $msg.clone().show(),
    transient: false,
    position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
    dismissable: false,
    relativeToBody: true
    });
    },
    function() {
    StackExchange.helpers.removeMessages();
    }
    );
    });
    });
    2 days ago


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

















      up vote
      28
      down vote

      favorite
      4









      up vote
      28
      down vote

      favorite
      4






      4






      This question already has an answer here:




      • Execution of Java static blocks in subclasses

        2 answers




      When I invoke the static variable y by using Checks.y (Checks being a subclass), the static block is not executed and the value of y doesn't get updated.



      class Par {
      static int y = 4;
      }

      class Checks extends Par {
      static {
      y = 5;
      }
      }

      public class Check {
      public static void main(String args) {
      System.out.println(Checks.y); // here printing 4
      }
      }


      As static is shared among all subclasses, the value is supposed to be updated.



      What could be the reason behind it?










      share|improve this question
















      This question already has an answer here:




      • Execution of Java static blocks in subclasses

        2 answers




      When I invoke the static variable y by using Checks.y (Checks being a subclass), the static block is not executed and the value of y doesn't get updated.



      class Par {
      static int y = 4;
      }

      class Checks extends Par {
      static {
      y = 5;
      }
      }

      public class Check {
      public static void main(String args) {
      System.out.println(Checks.y); // here printing 4
      }
      }


      As static is shared among all subclasses, the value is supposed to be updated.



      What could be the reason behind it?





      This question already has an answer here:




      • Execution of Java static blocks in subclasses

        2 answers








      java inheritance jvm static-variables static-block






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 4 at 16:12









      Yogu

      6,07822344




      6,07822344










      asked Nov 4 at 8:25









      rawat

      16816




      16816




      marked as duplicate by Oleksandr, Yogu, Krease, Mark Rotteveel java
      Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      2 days ago


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






      marked as duplicate by Oleksandr, Yogu, Krease, Mark Rotteveel java
      Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      2 days ago


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


























          8 Answers
          8






          active

          oldest

          votes

















          up vote
          26
          down vote



          accepted










          The field y is not declared by class Checks.



          Reading static fields doesn't trigger initialization of the referenced class (Checks), unless that class is the one in which the field is declared (see JLS quote below). In this example, even if y is accessed through Checks, that will only trigger the initialization of Par because Par is the class declaring y.



          In other words, the class Checks is in a sense not used at runtime.



          This is perhaps one illustration of why it's wrong to access static members through subclasses, something that causes a compile-time warning.





          There's a simple explanation in the specification:




          12.4.1. When Initialization Occurs



          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.


          • 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 (§7.6) and an assert statement (§14.10)
            lexically nested within T (§8.1.3) is executed.


            ...

            A reference to a static field (§8.3.1.1) causes initialization of only the class or interface that actually declares it, even though it might be referred to through the name of a subclass, a subinterface, or a class that implements an interface.





          The last note explains why your subclass is not being initialized.






          share|improve this answer























          • Thanks! I missed the point that initialization occurs only if the static field is declared by the "Check"(child) class itself! If I would have declared y as static in child class then the child's y hides the parent's( Par ) y?
            – rawat
            Nov 4 at 13:31








          • 1




            You mean Checks or really Check?
            – Jean-François Savard
            Nov 4 at 17:06






          • 1




            @Jean-FrançoisSavard Thanks, I meant Checks. The class names were changed at some point and I got it wrong while editing.
            – ernest_k
            Nov 4 at 17:09










          • @rawat Yes, the child's field would hide the parent's. It's another bad thing to redeclare field names in subclasses, for many reasons similar to this. If you need the field to be reassigned while the child class is being initialized, then maybe the best option is to acces the value through a static method in the subclass
            – ernest_k
            Nov 4 at 17:15










          • @ernest_k Thanks, I actually meant "Checks" as it is the child class of "Par" class, BTW ..sorry for the typo!
            – rawat
            2 days ago


















          up vote
          8
          down vote













          From JLS 12.4.1:




          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 (§7.6), and an assert statement (§14.10) lexically nested within T (§8.1.3) is executed.




          Since y is not declared in checks, none of the above criteria is satisfied.



          Another way to illustrate this behavior:



          class par {
          static int y = 4;
          static {
          System.out.println("static constructor of par");
          }
          }

          class checks extends par {
          static int x = 6;
          static {
          System.out.println("checks static constructor");
          y = 5;
          }
          }

          public class check{
          public static void main(String args){
          System.out.println(checks.y);
          System.out.println(checks.x);
          System.out.println(checks.y);
          }
          }


          Output



          static constructor of par
          4
          checks static constructor
          6
          5


          So after invoking checks.x that satisfies the second rule, the static constructor gets invoked.






          share|improve this answer






























            up vote
            4
            down vote













            Here:



            System.out.println(checks.y); // Here printing 4


            y refers to a field of the par class. This field access results into the loading of the par class (the parent class) according to the JLS (emphasis is mine):




            12.4. Initialization of Classes and Interfaces



            ....



            12.4.1. When Initialization Occurs



            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. 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 (§7.6) and an assert statement (§14.10)
            lexically nested within T (§8.1.3) is executed.




            But it doesn't load the checks class because (emphasis is mine) :




            A reference to a static field (§8.3.1.1) causes initialization of only
            the class or interface that actually declares it
            , even though it might
            be referred to through the name of a subclass, a subinterface, or a
            class that implements an interface.







            share|improve this answer






























              up vote
              3
              down vote













              That's because the static block in the checks class doesn't get executed. Although you mention the class checks the JVM doesn't load it at all.



              You can confirm this by adding another System.out.println inside the static block.



              class checks extends par {

              static {
              System.out.println("Test");
              y = 5;
              }
              }


              The word Test will never get printed.





              Read the section 12.4.1. When Initialization Occurs of the Java Language Specification to know more.






              share|improve this answer






























                up vote
                1
                down vote













                The one aspect not mentioned so far that might be confusing to new Java programmers: the fact how you organize your source code doesn't matter to a certain degree!



                You have your two classes (which should be name Parent and Child btw to follow Java naming conventions) in one file, or one piece of example. So you probably assume: these things come together automatically at runtime.



                But at runtime, there are individual class files per class. And as the others have said: nothing in the code references the child class. Thus that class isn't loaded, thus the assignment doesn't occur!






                share|improve this answer




























                  up vote
                  0
                  down vote













                  As others mention, the static block isn't executed because the class isn't initialized as answered before.



                  Notice the class convention names start with an upper case letter.



                  A clearer example to show Overriding class isn't used:



                  class Par {
                  static int y = 4;
                  public static void main(String args) {
                  System.out.println(Checks.y); // Here printing 4
                  System.out.println(new Checks().y); // Here printing 5
                  }
                  }

                  class Checks extends Par {
                  static {
                  y = 5;
                  }
                  }





                  share|improve this answer






























                    up vote
                    -1
                    down vote













                    As per your example, the class Check's static block never get called. Static blocks are always run before the object is created. If you add checks, object = new checks(), in your Check class you should see the expected value.






                    share|improve this answer























                    • "Check static block never get called." is just describing problem in question using different words. Real question is "why it isn't executed here".
                      – Pshemo
                      Nov 4 at 9:22










                    • "Static blocks are always run before the object is created" is unclear, do you mean that it happens every time before creating each object?
                      – Pshemo
                      Nov 4 at 9:26


















                    up vote
                    -1
                    down vote













                    Here is a variant of how you force the initialization of Checks class.



                    class Par {
                    static int y = 4;
                    }

                    class Checks extends Par {
                    public static int x;
                    static {
                    y = 5;
                    }
                    }

                    class Check {
                    public static void main(String args) {
                    System.out.println(checks.y); // Prints 4
                    System.out.println(checks.x); // Prints 0
                    System.out.println(checks.y); // Prints 5
                    }
                    }





                    share|improve this answer






























                      8 Answers
                      8






                      active

                      oldest

                      votes








                      8 Answers
                      8






                      active

                      oldest

                      votes









                      active

                      oldest

                      votes






                      active

                      oldest

                      votes








                      up vote
                      26
                      down vote



                      accepted










                      The field y is not declared by class Checks.



                      Reading static fields doesn't trigger initialization of the referenced class (Checks), unless that class is the one in which the field is declared (see JLS quote below). In this example, even if y is accessed through Checks, that will only trigger the initialization of Par because Par is the class declaring y.



                      In other words, the class Checks is in a sense not used at runtime.



                      This is perhaps one illustration of why it's wrong to access static members through subclasses, something that causes a compile-time warning.





                      There's a simple explanation in the specification:




                      12.4.1. When Initialization Occurs



                      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.


                      • 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 (§7.6) and an assert statement (§14.10)
                        lexically nested within T (§8.1.3) is executed.


                        ...

                        A reference to a static field (§8.3.1.1) causes initialization of only the class or interface that actually declares it, even though it might be referred to through the name of a subclass, a subinterface, or a class that implements an interface.





                      The last note explains why your subclass is not being initialized.






                      share|improve this answer























                      • Thanks! I missed the point that initialization occurs only if the static field is declared by the "Check"(child) class itself! If I would have declared y as static in child class then the child's y hides the parent's( Par ) y?
                        – rawat
                        Nov 4 at 13:31








                      • 1




                        You mean Checks or really Check?
                        – Jean-François Savard
                        Nov 4 at 17:06






                      • 1




                        @Jean-FrançoisSavard Thanks, I meant Checks. The class names were changed at some point and I got it wrong while editing.
                        – ernest_k
                        Nov 4 at 17:09










                      • @rawat Yes, the child's field would hide the parent's. It's another bad thing to redeclare field names in subclasses, for many reasons similar to this. If you need the field to be reassigned while the child class is being initialized, then maybe the best option is to acces the value through a static method in the subclass
                        – ernest_k
                        Nov 4 at 17:15










                      • @ernest_k Thanks, I actually meant "Checks" as it is the child class of "Par" class, BTW ..sorry for the typo!
                        – rawat
                        2 days ago















                      up vote
                      26
                      down vote



                      accepted










                      The field y is not declared by class Checks.



                      Reading static fields doesn't trigger initialization of the referenced class (Checks), unless that class is the one in which the field is declared (see JLS quote below). In this example, even if y is accessed through Checks, that will only trigger the initialization of Par because Par is the class declaring y.



                      In other words, the class Checks is in a sense not used at runtime.



                      This is perhaps one illustration of why it's wrong to access static members through subclasses, something that causes a compile-time warning.





                      There's a simple explanation in the specification:




                      12.4.1. When Initialization Occurs



                      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.


                      • 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 (§7.6) and an assert statement (§14.10)
                        lexically nested within T (§8.1.3) is executed.


                        ...

                        A reference to a static field (§8.3.1.1) causes initialization of only the class or interface that actually declares it, even though it might be referred to through the name of a subclass, a subinterface, or a class that implements an interface.





                      The last note explains why your subclass is not being initialized.






                      share|improve this answer























                      • Thanks! I missed the point that initialization occurs only if the static field is declared by the "Check"(child) class itself! If I would have declared y as static in child class then the child's y hides the parent's( Par ) y?
                        – rawat
                        Nov 4 at 13:31








                      • 1




                        You mean Checks or really Check?
                        – Jean-François Savard
                        Nov 4 at 17:06






                      • 1




                        @Jean-FrançoisSavard Thanks, I meant Checks. The class names were changed at some point and I got it wrong while editing.
                        – ernest_k
                        Nov 4 at 17:09










                      • @rawat Yes, the child's field would hide the parent's. It's another bad thing to redeclare field names in subclasses, for many reasons similar to this. If you need the field to be reassigned while the child class is being initialized, then maybe the best option is to acces the value through a static method in the subclass
                        – ernest_k
                        Nov 4 at 17:15










                      • @ernest_k Thanks, I actually meant "Checks" as it is the child class of "Par" class, BTW ..sorry for the typo!
                        – rawat
                        2 days ago













                      up vote
                      26
                      down vote



                      accepted







                      up vote
                      26
                      down vote



                      accepted






                      The field y is not declared by class Checks.



                      Reading static fields doesn't trigger initialization of the referenced class (Checks), unless that class is the one in which the field is declared (see JLS quote below). In this example, even if y is accessed through Checks, that will only trigger the initialization of Par because Par is the class declaring y.



                      In other words, the class Checks is in a sense not used at runtime.



                      This is perhaps one illustration of why it's wrong to access static members through subclasses, something that causes a compile-time warning.





                      There's a simple explanation in the specification:




                      12.4.1. When Initialization Occurs



                      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.


                      • 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 (§7.6) and an assert statement (§14.10)
                        lexically nested within T (§8.1.3) is executed.


                        ...

                        A reference to a static field (§8.3.1.1) causes initialization of only the class or interface that actually declares it, even though it might be referred to through the name of a subclass, a subinterface, or a class that implements an interface.





                      The last note explains why your subclass is not being initialized.






                      share|improve this answer














                      The field y is not declared by class Checks.



                      Reading static fields doesn't trigger initialization of the referenced class (Checks), unless that class is the one in which the field is declared (see JLS quote below). In this example, even if y is accessed through Checks, that will only trigger the initialization of Par because Par is the class declaring y.



                      In other words, the class Checks is in a sense not used at runtime.



                      This is perhaps one illustration of why it's wrong to access static members through subclasses, something that causes a compile-time warning.





                      There's a simple explanation in the specification:




                      12.4.1. When Initialization Occurs



                      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.


                      • 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 (§7.6) and an assert statement (§14.10)
                        lexically nested within T (§8.1.3) is executed.


                        ...

                        A reference to a static field (§8.3.1.1) causes initialization of only the class or interface that actually declares it, even though it might be referred to through the name of a subclass, a subinterface, or a class that implements an interface.





                      The last note explains why your subclass is not being initialized.







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Nov 4 at 17:08

























                      answered Nov 4 at 8:39









                      ernest_k

                      17.3k41835




                      17.3k41835












                      • Thanks! I missed the point that initialization occurs only if the static field is declared by the "Check"(child) class itself! If I would have declared y as static in child class then the child's y hides the parent's( Par ) y?
                        – rawat
                        Nov 4 at 13:31








                      • 1




                        You mean Checks or really Check?
                        – Jean-François Savard
                        Nov 4 at 17:06






                      • 1




                        @Jean-FrançoisSavard Thanks, I meant Checks. The class names were changed at some point and I got it wrong while editing.
                        – ernest_k
                        Nov 4 at 17:09










                      • @rawat Yes, the child's field would hide the parent's. It's another bad thing to redeclare field names in subclasses, for many reasons similar to this. If you need the field to be reassigned while the child class is being initialized, then maybe the best option is to acces the value through a static method in the subclass
                        – ernest_k
                        Nov 4 at 17:15










                      • @ernest_k Thanks, I actually meant "Checks" as it is the child class of "Par" class, BTW ..sorry for the typo!
                        – rawat
                        2 days ago


















                      • Thanks! I missed the point that initialization occurs only if the static field is declared by the "Check"(child) class itself! If I would have declared y as static in child class then the child's y hides the parent's( Par ) y?
                        – rawat
                        Nov 4 at 13:31








                      • 1




                        You mean Checks or really Check?
                        – Jean-François Savard
                        Nov 4 at 17:06






                      • 1




                        @Jean-FrançoisSavard Thanks, I meant Checks. The class names were changed at some point and I got it wrong while editing.
                        – ernest_k
                        Nov 4 at 17:09










                      • @rawat Yes, the child's field would hide the parent's. It's another bad thing to redeclare field names in subclasses, for many reasons similar to this. If you need the field to be reassigned while the child class is being initialized, then maybe the best option is to acces the value through a static method in the subclass
                        – ernest_k
                        Nov 4 at 17:15










                      • @ernest_k Thanks, I actually meant "Checks" as it is the child class of "Par" class, BTW ..sorry for the typo!
                        – rawat
                        2 days ago
















                      Thanks! I missed the point that initialization occurs only if the static field is declared by the "Check"(child) class itself! If I would have declared y as static in child class then the child's y hides the parent's( Par ) y?
                      – rawat
                      Nov 4 at 13:31






                      Thanks! I missed the point that initialization occurs only if the static field is declared by the "Check"(child) class itself! If I would have declared y as static in child class then the child's y hides the parent's( Par ) y?
                      – rawat
                      Nov 4 at 13:31






                      1




                      1




                      You mean Checks or really Check?
                      – Jean-François Savard
                      Nov 4 at 17:06




                      You mean Checks or really Check?
                      – Jean-François Savard
                      Nov 4 at 17:06




                      1




                      1




                      @Jean-FrançoisSavard Thanks, I meant Checks. The class names were changed at some point and I got it wrong while editing.
                      – ernest_k
                      Nov 4 at 17:09




                      @Jean-FrançoisSavard Thanks, I meant Checks. The class names were changed at some point and I got it wrong while editing.
                      – ernest_k
                      Nov 4 at 17:09












                      @rawat Yes, the child's field would hide the parent's. It's another bad thing to redeclare field names in subclasses, for many reasons similar to this. If you need the field to be reassigned while the child class is being initialized, then maybe the best option is to acces the value through a static method in the subclass
                      – ernest_k
                      Nov 4 at 17:15




                      @rawat Yes, the child's field would hide the parent's. It's another bad thing to redeclare field names in subclasses, for many reasons similar to this. If you need the field to be reassigned while the child class is being initialized, then maybe the best option is to acces the value through a static method in the subclass
                      – ernest_k
                      Nov 4 at 17:15












                      @ernest_k Thanks, I actually meant "Checks" as it is the child class of "Par" class, BTW ..sorry for the typo!
                      – rawat
                      2 days ago




                      @ernest_k Thanks, I actually meant "Checks" as it is the child class of "Par" class, BTW ..sorry for the typo!
                      – rawat
                      2 days ago












                      up vote
                      8
                      down vote













                      From JLS 12.4.1:




                      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 (§7.6), and an assert statement (§14.10) lexically nested within T (§8.1.3) is executed.




                      Since y is not declared in checks, none of the above criteria is satisfied.



                      Another way to illustrate this behavior:



                      class par {
                      static int y = 4;
                      static {
                      System.out.println("static constructor of par");
                      }
                      }

                      class checks extends par {
                      static int x = 6;
                      static {
                      System.out.println("checks static constructor");
                      y = 5;
                      }
                      }

                      public class check{
                      public static void main(String args){
                      System.out.println(checks.y);
                      System.out.println(checks.x);
                      System.out.println(checks.y);
                      }
                      }


                      Output



                      static constructor of par
                      4
                      checks static constructor
                      6
                      5


                      So after invoking checks.x that satisfies the second rule, the static constructor gets invoked.






                      share|improve this answer



























                        up vote
                        8
                        down vote













                        From JLS 12.4.1:




                        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 (§7.6), and an assert statement (§14.10) lexically nested within T (§8.1.3) is executed.




                        Since y is not declared in checks, none of the above criteria is satisfied.



                        Another way to illustrate this behavior:



                        class par {
                        static int y = 4;
                        static {
                        System.out.println("static constructor of par");
                        }
                        }

                        class checks extends par {
                        static int x = 6;
                        static {
                        System.out.println("checks static constructor");
                        y = 5;
                        }
                        }

                        public class check{
                        public static void main(String args){
                        System.out.println(checks.y);
                        System.out.println(checks.x);
                        System.out.println(checks.y);
                        }
                        }


                        Output



                        static constructor of par
                        4
                        checks static constructor
                        6
                        5


                        So after invoking checks.x that satisfies the second rule, the static constructor gets invoked.






                        share|improve this answer

























                          up vote
                          8
                          down vote










                          up vote
                          8
                          down vote









                          From JLS 12.4.1:




                          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 (§7.6), and an assert statement (§14.10) lexically nested within T (§8.1.3) is executed.




                          Since y is not declared in checks, none of the above criteria is satisfied.



                          Another way to illustrate this behavior:



                          class par {
                          static int y = 4;
                          static {
                          System.out.println("static constructor of par");
                          }
                          }

                          class checks extends par {
                          static int x = 6;
                          static {
                          System.out.println("checks static constructor");
                          y = 5;
                          }
                          }

                          public class check{
                          public static void main(String args){
                          System.out.println(checks.y);
                          System.out.println(checks.x);
                          System.out.println(checks.y);
                          }
                          }


                          Output



                          static constructor of par
                          4
                          checks static constructor
                          6
                          5


                          So after invoking checks.x that satisfies the second rule, the static constructor gets invoked.






                          share|improve this answer














                          From JLS 12.4.1:




                          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 (§7.6), and an assert statement (§14.10) lexically nested within T (§8.1.3) is executed.




                          Since y is not declared in checks, none of the above criteria is satisfied.



                          Another way to illustrate this behavior:



                          class par {
                          static int y = 4;
                          static {
                          System.out.println("static constructor of par");
                          }
                          }

                          class checks extends par {
                          static int x = 6;
                          static {
                          System.out.println("checks static constructor");
                          y = 5;
                          }
                          }

                          public class check{
                          public static void main(String args){
                          System.out.println(checks.y);
                          System.out.println(checks.x);
                          System.out.println(checks.y);
                          }
                          }


                          Output



                          static constructor of par
                          4
                          checks static constructor
                          6
                          5


                          So after invoking checks.x that satisfies the second rule, the static constructor gets invoked.







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Nov 4 at 14:28









                          Peter Mortensen

                          13.2k1983111




                          13.2k1983111










                          answered Nov 4 at 8:47









                          Diadistis

                          10.6k12746




                          10.6k12746






















                              up vote
                              4
                              down vote













                              Here:



                              System.out.println(checks.y); // Here printing 4


                              y refers to a field of the par class. This field access results into the loading of the par class (the parent class) according to the JLS (emphasis is mine):




                              12.4. Initialization of Classes and Interfaces



                              ....



                              12.4.1. When Initialization Occurs



                              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. 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 (§7.6) and an assert statement (§14.10)
                              lexically nested within T (§8.1.3) is executed.




                              But it doesn't load the checks class because (emphasis is mine) :




                              A reference to a static field (§8.3.1.1) causes initialization of only
                              the class or interface that actually declares it
                              , even though it might
                              be referred to through the name of a subclass, a subinterface, or a
                              class that implements an interface.







                              share|improve this answer



























                                up vote
                                4
                                down vote













                                Here:



                                System.out.println(checks.y); // Here printing 4


                                y refers to a field of the par class. This field access results into the loading of the par class (the parent class) according to the JLS (emphasis is mine):




                                12.4. Initialization of Classes and Interfaces



                                ....



                                12.4.1. When Initialization Occurs



                                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. 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 (§7.6) and an assert statement (§14.10)
                                lexically nested within T (§8.1.3) is executed.




                                But it doesn't load the checks class because (emphasis is mine) :




                                A reference to a static field (§8.3.1.1) causes initialization of only
                                the class or interface that actually declares it
                                , even though it might
                                be referred to through the name of a subclass, a subinterface, or a
                                class that implements an interface.







                                share|improve this answer

























                                  up vote
                                  4
                                  down vote










                                  up vote
                                  4
                                  down vote









                                  Here:



                                  System.out.println(checks.y); // Here printing 4


                                  y refers to a field of the par class. This field access results into the loading of the par class (the parent class) according to the JLS (emphasis is mine):




                                  12.4. Initialization of Classes and Interfaces



                                  ....



                                  12.4.1. When Initialization Occurs



                                  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. 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 (§7.6) and an assert statement (§14.10)
                                  lexically nested within T (§8.1.3) is executed.




                                  But it doesn't load the checks class because (emphasis is mine) :




                                  A reference to a static field (§8.3.1.1) causes initialization of only
                                  the class or interface that actually declares it
                                  , even though it might
                                  be referred to through the name of a subclass, a subinterface, or a
                                  class that implements an interface.







                                  share|improve this answer














                                  Here:



                                  System.out.println(checks.y); // Here printing 4


                                  y refers to a field of the par class. This field access results into the loading of the par class (the parent class) according to the JLS (emphasis is mine):




                                  12.4. Initialization of Classes and Interfaces



                                  ....



                                  12.4.1. When Initialization Occurs



                                  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. 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 (§7.6) and an assert statement (§14.10)
                                  lexically nested within T (§8.1.3) is executed.




                                  But it doesn't load the checks class because (emphasis is mine) :




                                  A reference to a static field (§8.3.1.1) causes initialization of only
                                  the class or interface that actually declares it
                                  , even though it might
                                  be referred to through the name of a subclass, a subinterface, or a
                                  class that implements an interface.








                                  share|improve this answer














                                  share|improve this answer



                                  share|improve this answer








                                  edited Nov 4 at 14:33









                                  Peter Mortensen

                                  13.2k1983111




                                  13.2k1983111










                                  answered Nov 4 at 9:17









                                  davidxxx

                                  60.4k55083




                                  60.4k55083






















                                      up vote
                                      3
                                      down vote













                                      That's because the static block in the checks class doesn't get executed. Although you mention the class checks the JVM doesn't load it at all.



                                      You can confirm this by adding another System.out.println inside the static block.



                                      class checks extends par {

                                      static {
                                      System.out.println("Test");
                                      y = 5;
                                      }
                                      }


                                      The word Test will never get printed.





                                      Read the section 12.4.1. When Initialization Occurs of the Java Language Specification to know more.






                                      share|improve this answer



























                                        up vote
                                        3
                                        down vote













                                        That's because the static block in the checks class doesn't get executed. Although you mention the class checks the JVM doesn't load it at all.



                                        You can confirm this by adding another System.out.println inside the static block.



                                        class checks extends par {

                                        static {
                                        System.out.println("Test");
                                        y = 5;
                                        }
                                        }


                                        The word Test will never get printed.





                                        Read the section 12.4.1. When Initialization Occurs of the Java Language Specification to know more.






                                        share|improve this answer

























                                          up vote
                                          3
                                          down vote










                                          up vote
                                          3
                                          down vote









                                          That's because the static block in the checks class doesn't get executed. Although you mention the class checks the JVM doesn't load it at all.



                                          You can confirm this by adding another System.out.println inside the static block.



                                          class checks extends par {

                                          static {
                                          System.out.println("Test");
                                          y = 5;
                                          }
                                          }


                                          The word Test will never get printed.





                                          Read the section 12.4.1. When Initialization Occurs of the Java Language Specification to know more.






                                          share|improve this answer














                                          That's because the static block in the checks class doesn't get executed. Although you mention the class checks the JVM doesn't load it at all.



                                          You can confirm this by adding another System.out.println inside the static block.



                                          class checks extends par {

                                          static {
                                          System.out.println("Test");
                                          y = 5;
                                          }
                                          }


                                          The word Test will never get printed.





                                          Read the section 12.4.1. When Initialization Occurs of the Java Language Specification to know more.







                                          share|improve this answer














                                          share|improve this answer



                                          share|improve this answer








                                          edited Nov 4 at 9:06

























                                          answered Nov 4 at 8:43









                                          Roshana Pitigala

                                          4,38562346




                                          4,38562346






















                                              up vote
                                              1
                                              down vote













                                              The one aspect not mentioned so far that might be confusing to new Java programmers: the fact how you organize your source code doesn't matter to a certain degree!



                                              You have your two classes (which should be name Parent and Child btw to follow Java naming conventions) in one file, or one piece of example. So you probably assume: these things come together automatically at runtime.



                                              But at runtime, there are individual class files per class. And as the others have said: nothing in the code references the child class. Thus that class isn't loaded, thus the assignment doesn't occur!






                                              share|improve this answer

























                                                up vote
                                                1
                                                down vote













                                                The one aspect not mentioned so far that might be confusing to new Java programmers: the fact how you organize your source code doesn't matter to a certain degree!



                                                You have your two classes (which should be name Parent and Child btw to follow Java naming conventions) in one file, or one piece of example. So you probably assume: these things come together automatically at runtime.



                                                But at runtime, there are individual class files per class. And as the others have said: nothing in the code references the child class. Thus that class isn't loaded, thus the assignment doesn't occur!






                                                share|improve this answer























                                                  up vote
                                                  1
                                                  down vote










                                                  up vote
                                                  1
                                                  down vote









                                                  The one aspect not mentioned so far that might be confusing to new Java programmers: the fact how you organize your source code doesn't matter to a certain degree!



                                                  You have your two classes (which should be name Parent and Child btw to follow Java naming conventions) in one file, or one piece of example. So you probably assume: these things come together automatically at runtime.



                                                  But at runtime, there are individual class files per class. And as the others have said: nothing in the code references the child class. Thus that class isn't loaded, thus the assignment doesn't occur!






                                                  share|improve this answer












                                                  The one aspect not mentioned so far that might be confusing to new Java programmers: the fact how you organize your source code doesn't matter to a certain degree!



                                                  You have your two classes (which should be name Parent and Child btw to follow Java naming conventions) in one file, or one piece of example. So you probably assume: these things come together automatically at runtime.



                                                  But at runtime, there are individual class files per class. And as the others have said: nothing in the code references the child class. Thus that class isn't loaded, thus the assignment doesn't occur!







                                                  share|improve this answer












                                                  share|improve this answer



                                                  share|improve this answer










                                                  answered Nov 4 at 9:59









                                                  GhostCat

                                                  85k1580139




                                                  85k1580139






















                                                      up vote
                                                      0
                                                      down vote













                                                      As others mention, the static block isn't executed because the class isn't initialized as answered before.



                                                      Notice the class convention names start with an upper case letter.



                                                      A clearer example to show Overriding class isn't used:



                                                      class Par {
                                                      static int y = 4;
                                                      public static void main(String args) {
                                                      System.out.println(Checks.y); // Here printing 4
                                                      System.out.println(new Checks().y); // Here printing 5
                                                      }
                                                      }

                                                      class Checks extends Par {
                                                      static {
                                                      y = 5;
                                                      }
                                                      }





                                                      share|improve this answer



























                                                        up vote
                                                        0
                                                        down vote













                                                        As others mention, the static block isn't executed because the class isn't initialized as answered before.



                                                        Notice the class convention names start with an upper case letter.



                                                        A clearer example to show Overriding class isn't used:



                                                        class Par {
                                                        static int y = 4;
                                                        public static void main(String args) {
                                                        System.out.println(Checks.y); // Here printing 4
                                                        System.out.println(new Checks().y); // Here printing 5
                                                        }
                                                        }

                                                        class Checks extends Par {
                                                        static {
                                                        y = 5;
                                                        }
                                                        }





                                                        share|improve this answer

























                                                          up vote
                                                          0
                                                          down vote










                                                          up vote
                                                          0
                                                          down vote









                                                          As others mention, the static block isn't executed because the class isn't initialized as answered before.



                                                          Notice the class convention names start with an upper case letter.



                                                          A clearer example to show Overriding class isn't used:



                                                          class Par {
                                                          static int y = 4;
                                                          public static void main(String args) {
                                                          System.out.println(Checks.y); // Here printing 4
                                                          System.out.println(new Checks().y); // Here printing 5
                                                          }
                                                          }

                                                          class Checks extends Par {
                                                          static {
                                                          y = 5;
                                                          }
                                                          }





                                                          share|improve this answer














                                                          As others mention, the static block isn't executed because the class isn't initialized as answered before.



                                                          Notice the class convention names start with an upper case letter.



                                                          A clearer example to show Overriding class isn't used:



                                                          class Par {
                                                          static int y = 4;
                                                          public static void main(String args) {
                                                          System.out.println(Checks.y); // Here printing 4
                                                          System.out.println(new Checks().y); // Here printing 5
                                                          }
                                                          }

                                                          class Checks extends Par {
                                                          static {
                                                          y = 5;
                                                          }
                                                          }






                                                          share|improve this answer














                                                          share|improve this answer



                                                          share|improve this answer








                                                          edited Nov 4 at 14:30









                                                          Peter Mortensen

                                                          13.2k1983111




                                                          13.2k1983111










                                                          answered Nov 4 at 8:53









                                                          user7294900

                                                          16.8k92954




                                                          16.8k92954






















                                                              up vote
                                                              -1
                                                              down vote













                                                              As per your example, the class Check's static block never get called. Static blocks are always run before the object is created. If you add checks, object = new checks(), in your Check class you should see the expected value.






                                                              share|improve this answer























                                                              • "Check static block never get called." is just describing problem in question using different words. Real question is "why it isn't executed here".
                                                                – Pshemo
                                                                Nov 4 at 9:22










                                                              • "Static blocks are always run before the object is created" is unclear, do you mean that it happens every time before creating each object?
                                                                – Pshemo
                                                                Nov 4 at 9:26















                                                              up vote
                                                              -1
                                                              down vote













                                                              As per your example, the class Check's static block never get called. Static blocks are always run before the object is created. If you add checks, object = new checks(), in your Check class you should see the expected value.






                                                              share|improve this answer























                                                              • "Check static block never get called." is just describing problem in question using different words. Real question is "why it isn't executed here".
                                                                – Pshemo
                                                                Nov 4 at 9:22










                                                              • "Static blocks are always run before the object is created" is unclear, do you mean that it happens every time before creating each object?
                                                                – Pshemo
                                                                Nov 4 at 9:26













                                                              up vote
                                                              -1
                                                              down vote










                                                              up vote
                                                              -1
                                                              down vote









                                                              As per your example, the class Check's static block never get called. Static blocks are always run before the object is created. If you add checks, object = new checks(), in your Check class you should see the expected value.






                                                              share|improve this answer














                                                              As per your example, the class Check's static block never get called. Static blocks are always run before the object is created. If you add checks, object = new checks(), in your Check class you should see the expected value.







                                                              share|improve this answer














                                                              share|improve this answer



                                                              share|improve this answer








                                                              edited Nov 4 at 14:34









                                                              Peter Mortensen

                                                              13.2k1983111




                                                              13.2k1983111










                                                              answered Nov 4 at 9:18









                                                              Namal Dinesh Ubhayawardana

                                                              1952




                                                              1952












                                                              • "Check static block never get called." is just describing problem in question using different words. Real question is "why it isn't executed here".
                                                                – Pshemo
                                                                Nov 4 at 9:22










                                                              • "Static blocks are always run before the object is created" is unclear, do you mean that it happens every time before creating each object?
                                                                – Pshemo
                                                                Nov 4 at 9:26


















                                                              • "Check static block never get called." is just describing problem in question using different words. Real question is "why it isn't executed here".
                                                                – Pshemo
                                                                Nov 4 at 9:22










                                                              • "Static blocks are always run before the object is created" is unclear, do you mean that it happens every time before creating each object?
                                                                – Pshemo
                                                                Nov 4 at 9:26
















                                                              "Check static block never get called." is just describing problem in question using different words. Real question is "why it isn't executed here".
                                                              – Pshemo
                                                              Nov 4 at 9:22




                                                              "Check static block never get called." is just describing problem in question using different words. Real question is "why it isn't executed here".
                                                              – Pshemo
                                                              Nov 4 at 9:22












                                                              "Static blocks are always run before the object is created" is unclear, do you mean that it happens every time before creating each object?
                                                              – Pshemo
                                                              Nov 4 at 9:26




                                                              "Static blocks are always run before the object is created" is unclear, do you mean that it happens every time before creating each object?
                                                              – Pshemo
                                                              Nov 4 at 9:26










                                                              up vote
                                                              -1
                                                              down vote













                                                              Here is a variant of how you force the initialization of Checks class.



                                                              class Par {
                                                              static int y = 4;
                                                              }

                                                              class Checks extends Par {
                                                              public static int x;
                                                              static {
                                                              y = 5;
                                                              }
                                                              }

                                                              class Check {
                                                              public static void main(String args) {
                                                              System.out.println(checks.y); // Prints 4
                                                              System.out.println(checks.x); // Prints 0
                                                              System.out.println(checks.y); // Prints 5
                                                              }
                                                              }





                                                              share|improve this answer



























                                                                up vote
                                                                -1
                                                                down vote













                                                                Here is a variant of how you force the initialization of Checks class.



                                                                class Par {
                                                                static int y = 4;
                                                                }

                                                                class Checks extends Par {
                                                                public static int x;
                                                                static {
                                                                y = 5;
                                                                }
                                                                }

                                                                class Check {
                                                                public static void main(String args) {
                                                                System.out.println(checks.y); // Prints 4
                                                                System.out.println(checks.x); // Prints 0
                                                                System.out.println(checks.y); // Prints 5
                                                                }
                                                                }





                                                                share|improve this answer

























                                                                  up vote
                                                                  -1
                                                                  down vote










                                                                  up vote
                                                                  -1
                                                                  down vote









                                                                  Here is a variant of how you force the initialization of Checks class.



                                                                  class Par {
                                                                  static int y = 4;
                                                                  }

                                                                  class Checks extends Par {
                                                                  public static int x;
                                                                  static {
                                                                  y = 5;
                                                                  }
                                                                  }

                                                                  class Check {
                                                                  public static void main(String args) {
                                                                  System.out.println(checks.y); // Prints 4
                                                                  System.out.println(checks.x); // Prints 0
                                                                  System.out.println(checks.y); // Prints 5
                                                                  }
                                                                  }





                                                                  share|improve this answer














                                                                  Here is a variant of how you force the initialization of Checks class.



                                                                  class Par {
                                                                  static int y = 4;
                                                                  }

                                                                  class Checks extends Par {
                                                                  public static int x;
                                                                  static {
                                                                  y = 5;
                                                                  }
                                                                  }

                                                                  class Check {
                                                                  public static void main(String args) {
                                                                  System.out.println(checks.y); // Prints 4
                                                                  System.out.println(checks.x); // Prints 0
                                                                  System.out.println(checks.y); // Prints 5
                                                                  }
                                                                  }






                                                                  share|improve this answer














                                                                  share|improve this answer



                                                                  share|improve this answer








                                                                  edited Nov 4 at 14:35









                                                                  Peter Mortensen

                                                                  13.2k1983111




                                                                  13.2k1983111










                                                                  answered Nov 4 at 9:41









                                                                  Murat Karagöz

                                                                  13.4k33263




                                                                  13.4k33263















                                                                      這個網誌中的熱門文章

                                                                      Tangent Lines Diagram Along Smooth Curve

                                                                      Yusuf al-Mu'taman ibn Hud

                                                                      Zucchini