Java myth: Static code blocks, when the class will be loaded into its own initiative to run?

Source: Internet
Author: User

the Java static code block will run itself when the class is loaded?

The idea of a lot of Java developers was deeply raped by this thought N times, spreading the wrong thought of the blog, in a heap of online, more and more people were 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 must be loaded into the virtual machine};p ublic static void Main (string[] args) { System.out.println ("Hello word");}}

Run result: no "static Bolck" output


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

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



Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.

Java myth: Static code blocks, when the class will be loaded into its own initiative to run?

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.