Value of static variable not changed even after initializing the child class in java?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
12
down vote

favorite












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 I invoked y static variable using the subclass.y still static block is not executing and value of y didn't get updated.



what could be the reason behind it?



As static is shared among all subclasses so the value is supposed to be updated.
where am I getting wrong?










share|improve this question























  • I know this comment is just noise, but it's worth saying. Excellent Question!
    – Roshana Pitigala
    15 mins ago















up vote
12
down vote

favorite












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 I invoked y static variable using the subclass.y still static block is not executing and value of y didn't get updated.



what could be the reason behind it?



As static is shared among all subclasses so the value is supposed to be updated.
where am I getting wrong?










share|improve this question























  • I know this comment is just noise, but it's worth saying. Excellent Question!
    – Roshana Pitigala
    15 mins ago













up vote
12
down vote

favorite









up vote
12
down vote

favorite











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 I invoked y static variable using the subclass.y still static block is not executing and value of y didn't get updated.



what could be the reason behind it?



As static is shared among all subclasses so the value is supposed to be updated.
where am I getting wrong?










share|improve this question















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 I invoked y static variable using the subclass.y still static block is not executing and value of y didn't get updated.



what could be the reason behind it?



As static is shared among all subclasses so the value is supposed to be updated.
where am I getting wrong?







java inheritance jvm static-variables static-block






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 19 mins ago









Roshana Pitigala

4,31462345




4,31462345










asked 45 mins ago









rawat

754




754











  • I know this comment is just noise, but it's worth saying. Excellent Question!
    – Roshana Pitigala
    15 mins ago

















  • I know this comment is just noise, but it's worth saying. Excellent Question!
    – Roshana Pitigala
    15 mins ago
















I know this comment is just noise, but it's worth saying. Excellent Question!
– Roshana Pitigala
15 mins ago





I know this comment is just noise, but it's worth saying. Excellent Question!
– Roshana Pitigala
15 mins ago













5 Answers
5






active

oldest

votes

















up vote
2
down vote













The field y does not belong to class checks.



Even if it's accessed with checks.y, the compiler surely knows that the only class being used in all of this is par.



In other words, the class checks is not being used at runtime.



This is an example of the wrong of accessing static members through subclasses, for which you get a compile-time warning.






share|improve this answer



























    up vote
    1
    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("par static constructor");



    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



    par static constructor
    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
      1
      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
        0
        down vote













        It seems the Classloader performs some optimization here, and since it determines that 'checks' isn't really used, it skips running its static block.
        If you force checks to be loaded, e.g., by instantiating an object of that type, you'll indeed see 5 printed.






        share|improve this answer




















        • checks can be utility class that doesn't need to be instantiate
          – user7294900
          35 mins ago

















        up vote
        0
        down vote













        As other mention, static block isn't executed because class isn't initialized as answered before,



        Notice the class convention names start with an upper case



        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






















          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53138984%2fvalue-of-static-variable-not-changed-even-after-initializing-the-child-class-in%23new-answer', 'question_page');

          );

          Post as a guest






























          5 Answers
          5






          active

          oldest

          votes








          5 Answers
          5






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          2
          down vote













          The field y does not belong to class checks.



          Even if it's accessed with checks.y, the compiler surely knows that the only class being used in all of this is par.



          In other words, the class checks is not being used at runtime.



          This is an example of the wrong of accessing static members through subclasses, for which you get a compile-time warning.






          share|improve this answer
























            up vote
            2
            down vote













            The field y does not belong to class checks.



            Even if it's accessed with checks.y, the compiler surely knows that the only class being used in all of this is par.



            In other words, the class checks is not being used at runtime.



            This is an example of the wrong of accessing static members through subclasses, for which you get a compile-time warning.






            share|improve this answer






















              up vote
              2
              down vote










              up vote
              2
              down vote









              The field y does not belong to class checks.



              Even if it's accessed with checks.y, the compiler surely knows that the only class being used in all of this is par.



              In other words, the class checks is not being used at runtime.



              This is an example of the wrong of accessing static members through subclasses, for which you get a compile-time warning.






              share|improve this answer












              The field y does not belong to class checks.



              Even if it's accessed with checks.y, the compiler surely knows that the only class being used in all of this is par.



              In other words, the class checks is not being used at runtime.



              This is an example of the wrong of accessing static members through subclasses, for which you get a compile-time warning.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered 31 mins ago









              ernest_k

              16.8k41533




              16.8k41533






















                  up vote
                  1
                  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("par static constructor");



                  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



                  par static constructor
                  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
                    1
                    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("par static constructor");



                    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



                    par static constructor
                    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
                      1
                      down vote










                      up vote
                      1
                      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("par static constructor");



                      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



                      par static constructor
                      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("par static constructor");



                      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



                      par static constructor
                      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










                      answered 23 mins ago









                      Diadistis

                      10.5k12746




                      10.5k12746




















                          up vote
                          1
                          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













                            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










                              up vote
                              1
                              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 4 mins ago

























                              answered 27 mins ago









                              Roshana Pitigala

                              4,31462345




                              4,31462345




















                                  up vote
                                  0
                                  down vote













                                  It seems the Classloader performs some optimization here, and since it determines that 'checks' isn't really used, it skips running its static block.
                                  If you force checks to be loaded, e.g., by instantiating an object of that type, you'll indeed see 5 printed.






                                  share|improve this answer




















                                  • checks can be utility class that doesn't need to be instantiate
                                    – user7294900
                                    35 mins ago














                                  up vote
                                  0
                                  down vote













                                  It seems the Classloader performs some optimization here, and since it determines that 'checks' isn't really used, it skips running its static block.
                                  If you force checks to be loaded, e.g., by instantiating an object of that type, you'll indeed see 5 printed.






                                  share|improve this answer




















                                  • checks can be utility class that doesn't need to be instantiate
                                    – user7294900
                                    35 mins ago












                                  up vote
                                  0
                                  down vote










                                  up vote
                                  0
                                  down vote









                                  It seems the Classloader performs some optimization here, and since it determines that 'checks' isn't really used, it skips running its static block.
                                  If you force checks to be loaded, e.g., by instantiating an object of that type, you'll indeed see 5 printed.






                                  share|improve this answer












                                  It seems the Classloader performs some optimization here, and since it determines that 'checks' isn't really used, it skips running its static block.
                                  If you force checks to be loaded, e.g., by instantiating an object of that type, you'll indeed see 5 printed.







                                  share|improve this answer












                                  share|improve this answer



                                  share|improve this answer










                                  answered 37 mins ago









                                  Mureinik

                                  173k21122190




                                  173k21122190











                                  • checks can be utility class that doesn't need to be instantiate
                                    – user7294900
                                    35 mins ago
















                                  • checks can be utility class that doesn't need to be instantiate
                                    – user7294900
                                    35 mins ago















                                  checks can be utility class that doesn't need to be instantiate
                                  – user7294900
                                  35 mins ago




                                  checks can be utility class that doesn't need to be instantiate
                                  – user7294900
                                  35 mins ago










                                  up vote
                                  0
                                  down vote













                                  As other mention, static block isn't executed because class isn't initialized as answered before,



                                  Notice the class convention names start with an upper case



                                  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 other mention, static block isn't executed because class isn't initialized as answered before,



                                    Notice the class convention names start with an upper case



                                    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 other mention, static block isn't executed because class isn't initialized as answered before,



                                      Notice the class convention names start with an upper case



                                      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 other mention, static block isn't executed because class isn't initialized as answered before,



                                      Notice the class convention names start with an upper case



                                      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 9 mins ago

























                                      answered 17 mins ago









                                      user7294900

                                      16.2k92953




                                      16.2k92953



























                                           

                                          draft saved


                                          draft discarded















































                                           


                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function ()
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53138984%2fvalue-of-static-variable-not-changed-even-after-initializing-the-child-class-in%23new-answer', 'question_page');

                                          );

                                          Post as a guest













































































                                          Comments

                                          Popular posts from this blog

                                          What does second last employer means? [closed]

                                          List of Gilmore Girls characters

                                          Confectionery