Java Interview "class initialization + instantiation-strengthening" __java

Source: Internet
Author: User
Yang 77, you are more powerful than me ...

but I can be more you than you ... Anger Brush ...
        public static Test t1 = new Test ();
        public static Test t2 = new Test ();
        {
            System.out.println ("Building Block");
        }
        Static
        {
            System.out.println ("static Block");
        }
        public static void Main (string[] args)
        {
            Test t = new Test ();
        }
Output: The

building block
building block
static block
//top three is the output after initializing the class, followed by the output of new in Main method ...
Building Blocks
Answer
Yang 77 asked me this question, I made a mistake. Hey MD, no careful analysis ...

key point: The initialization of the class is not triggered when the class is initialized ...

Detailed analysis:

in the Main method new test, experienced the following process:

1. Class loading:

-After this step completes, the static member variable has been initialized to the default value. If the Boolen is False,int 0, the class variable is assigned null.
-The static member method has been loaded into the method area memory and can invoke execution ...

2. Class initialization:

-This stage executes the static code block and initializes the static member variable assignment to the target value set by the programmer. If, as in the case above, during the initialization class, instantiation of this class, then the instantiation of the process is still in accordance with the Convention, such as the implementation of non-static code blocks, initialization of non-static member variables, initialization of Non-static member variables is one-step, directly initialized to the programmer to assign the value, without the middle value of the said.

-In this particular case, this class is instantiated in the process of initialization, so it does not go into the second level of initialization like recursion.

after the initialization of the example class is finished, the constructor block

building block
static block

3. The last is the instantiation of the class in main, where the initialization of the

class is done and finally instantiated.

when the step is completed, it is output: the

building block
Summary:Static code block, the static member variable is initialized only once. Executing in the initialization of a class ... Non-static code blocks and non-static member variables are executed during instantiation, and each instantiation executes ... class initialization and class instantiation, the two processes are two independent of each other, two processes to perform the task is also separate ... The initialization of the class and the order in which the classes are instantiated are not necessarily the same, for example: When a class instantiation is complete, the initialization of the class is not necessarily complete. This class can be instantiated during class initialization ... one more.

This is the author came up with a, designed to help the reader to test whether really understand ...

        public static Test t1 = new Test ();
        public static Test t2 = new Test ();

        {
            System.out.println ("Building Block");
            Showstaticmethod ();
            if (t1!=null) {
                t1.show ();
            }
            SYSTEM.OUT.PRINTLN (number);
        }
        static {
            System.out.println ("static Block");
        }

        private static int number=10+10;

        public static void Showstaticmethod () {
            System.out.println ("Staticmethod executed!!");
        }
        Public  Void Show () {
            System.out.println ("Yang 77 ...)" ");
        }
        public static void Main (string[] args) {
            Test t = new Test ();
        }
Output:




construction block
staticmethod executed!!
0
Tectonic block
staticmethod executed!!
Yang 77 ...
0
Static block
construction block
staticmethod executed!!
Yang 77 ...
20
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.