-1

I am searching for a long time on net. But no use. Please help or try to give some ideas how to achieve this?why the result is not to print the "int main"in the first? What I want to know is why the result of this program is as follow?Thanks in advance.

super static block

static block 4

in main

super constructor

constructor

 class StaticSuper {
        static {
            System.out.println("super static block");   
        }


        StaticSuper() {
            System.out.println("super constructor");    
        }
    }

    public class StaticTests extends StaticSuper {
        static int rand;

        //static  initialise 
        static {           
        rand = (int) (Math.random() * 6);
        System.out.println("static block " + rand);     
        }   

        StaticTests() {
            System.out.println("constructor");  
        }

        public static void main(String[] args) {
            System.out.println("in main");
            StaticTests st = new StaticTests(); 
        }
    }
Manhand
  • 11
  • 3

2 Answers2

2

The firing order is:

  1. Parent static blocks and fields in order of appearance
  2. Child static blocks and fields in order of appearance
  3. Parent non-static field initializers
  4. Parent constructor

  5. Child non-static field initializers

  6. Child constructor

You can read more about here


Update:

Run this and you will understand why you should not call non-final method in super class constructor.

public class Derived extends Super

{

    @Override
    void initialise()
    {
        System.out.println("Now you can't initialise field \"a\" anymore");
    }
    Derived()
    {

    }
    public static void main(String[] args)
    {
        Derived d = new Derived();

    }
}

class Super
{
    private int a;
    void initialise()
    {
        a = 10;
    }
    Super()
    {
        initialise();
    }
}

So you can't initialise your field a anymore it might break your super class code.

final methods cannot be overridden. So, you can initialise field a and you won't break anything in your super class.

I hope it clarifies your doubt.

SkrewEverything
  • 2,393
  • 1
  • 19
  • 50
  • Thank you for your answer sincerely.I have been read your article and I have a question:the non_field of the subclass is initialized before we call the constructor of the subclass,is it right?so why can't we call the non_field from the constructor?I do not understand it. – Manhand Nov 20 '16 at 06:28
  • I feel so sorry about that,I type the question wrong.The question is why we shouldn't call non-final methods from the constructor(.I have been read your article and I have a question:the non_field of the subclass is initialized before we call the constructor of the subclass,is it right?so why can't we call the non-final methods from the constructor?I do not understand it) – Manhand Nov 20 '16 at 11:23
  • @Manhand I have updated my answer with code to explain why it is not recommended to call a non final method in your constructor. check it. – SkrewEverything Nov 22 '16 at 11:15
1

The sequence is as follows:

  1. You type java StaticTests

  2. The JVM loads the StaticTests.

  3. The JVM locates the static void main(String[]) method and attempts to call it.

  4. The call to main(...) triggers the static initialization of StaticTests. (A class is initialized before the first call to a static method.)

  5. The static initialization of StaticTests triggeres the static initialization of StaticSuper. (A classes superclasses must be initialized before the class is initialized.)

  6. The "super static block" is printed by the super static init.

  7. The "static block 4" is printed by the subclass static init.

  8. The main(...) call starts.

  9. The System.out.println("in main") statement is executed, printing "in main".

  10. The new StaticTests calls the StaticTests constructor.

  11. The StaticTests constructor calls super() implicitly. (Normal Java behavior.)

  12. The superclass constructor prints "super constructor".

  13. The subclass constructor prints "constructor"

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216