4 Replies Latest reply: Jan 18, 2014 3:41 PM by rp0428 RSS

    below program o/p was static, null. can you explain any one ?

    5009596

      class NewClass {
      static {

      out.println(NewClass.string+" ,"+NewClass.integer);

      }

      final static String string="static";

      final static Integer integer= 1 ;
      public static void main(String [] args)//throws Exception

      {

      }

      }


        • 1. Re: below program o/p was static, null. can you explain any one ?
          5009596

          Hi Wizzle ,

           

          In order to execution of java program first static variable are executed first and static block are executed in that case output was static,1 but why  it was giving output static,null ?

          • 2. Re: below program o/p was static, null. can you explain any one ?
            rp0428
            In order to execution of java program first static variable are executed first and static block are executed in that case output was static,1 but why  it was giving output static,null ?

            Because the description you posted is NOT quite correct.

            This is your line of code:

            final static String string="static";

            That creates a 'constant variable'. See section 4.12.4 'final Variables' in The Java Language Spec.

            http://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.12.4

            A variable of primitive type or type String, that is final and initialized with a compile-time constant expression (§15.28), is called a constant variable.

            Whether a variable is a constant variable or not may have implications with respect to class initialization (§12.4.1), binary compatibility (§13.1, §13.4.9) and definite assignment (§16). 

            See that last statement about 'may have implications with respect to class initialization'? Read the section 12.4.1 referred to:

            12.4.2. Detailed Initialization Procedure

            For each class or interface C, there is a unique initialization lock LC. The mapping from C to LC is left to the discretion of the Java Virtual Machine implementation. The procedure for initializing C is then as follows:

            . . .

            6. Otherwise, record the fact that initialization of the Class object for C is in progress by the current thread, and release LC

            Then, initialize the final class variables and fields of interfaces whose values are compile-time constant expressions (§8.3.2.1, §9.3.1, §13.4.9, §15.28).

             

            . . .

            9. Next, execute either the class variable initializers and static initializers of the class, or the field initializers of the interface, in textual order, as though they were a single block.

            . . .

            That 'constant variable' is initialized in step #6 of the initialization. Then, as step #9 says, 'the class variable initializers and static initializers' are executed 'in textual order'.

             

            This 'static intializer' line is FIRST in that 'textual order':

            System.out.println(NewClass.string+" ,"+NewClass.integer);

            At that point the 'string' static (which is a 'constant variable') has been initialized but the 'integer' has not so it is still NULL.

            • 3. Re: below program o/p was static, null. can you explain any one ?
              5009596

              thanks for your explanation

               

              In that above case if we create Object to string using new operator  it was giving null value even though 'string' was final and static variable. Can you explain to me ?

               

              final static String string=new String("static");

              • 4. Re: below program o/p was static, null. can you explain any one ?
                rp0428
                In that above case if we create Object to string using new operator  it was giving nullvalue even though 'string' was final and static variable. Can you explain to me ?

                 

                final static String string=new String("static");

                What I said above DOES explain it to you.

                 

                That new line of code is NOT a 'constant variable' so it will be NULL for the same reason the other one was null. Reread what I said above.