Java myth: Static blocks of code that are executed automatically when the class is loaded?

Source: Internet
Author: User

java static code blocks are automatically executed when the class is loaded?

A lot of Java developers thought, by this thought deeply gang raped n times, spread the wrong thought of the blog, in the online heap, more and more people are gang-raped.

such as: http://blog.csdn.net/leeyu35/article/details/7755304


Then our procedure is to prove that this is a false statement:

Class MyClass1 {static {//Static blocks System.out.println ("static Block");}} public class Main {class[] Classarray = {myclass1.class//This class is referenced, it is necessary to load the class into the virtual machine};p ublic static void Main (string[] args) { System.out.println ("Hello word");}}

Execution result: "Static Bolck" is not output


So when will the static block be called? I found an article that introduces a more detailed blog.

Http://www.cnblogs.com/ivanfu/archive/2012/02/12/2347817.html



Java myth: Static blocks of code that are executed automatically when the class is loaded?

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.