Understanding of Java code blocks

Source: Internet
Author: User

Recently in the review of Java Foundation, in the Java code block, suddenly found himself seemingly ignorant of Java code block, so hurriedly to some information on the actual combat exercise.

For Java code blocks, it is not difficult to see by name is actually a collection of some Java statements, in the form of {} , there are 4 forms:

1. Method body of the class

This is one of our most common Java code blocks, in the following form:

1  Public class Boke {2      Public void say () {3         System.out.println ("I am the content of the code block"); 4     }5 }

The method body of say {System.out.println ("I am the content of the code block");} Is a Java code block.

2. Building Code blocks

Not very common, it has the following form (7-10 lines):

1  Public classBoke {2     3      Public voidsay () {4System.out.println ("I'm the content of the code block."));5     }6     7     //Constructioncode block8     {9System.out.println ("I am an ordinary Java code block");Ten     } One}

In fact, I don't think I should call it a Java code block in general, because that would confuse the Java code block as I do, so I don't know exactly what the Java code blocks of this type are.

So, in the small title I specifically labeled the "have the real meaning" of the name- construction code block

So why is it called building blocks of code? Here's a little experiment:

Add a Main method and a construction method to the above program to allow the code to run with the following code:

1  Public classBoke {2     //method Body3      Public voidsay () {4System.out.println ("I'm the content of the code block."));5     }6     7     //common Java code blocks8     {9System.out.println ("I am an ordinary Java code block");Ten     } One      A      PublicBoke () { -System.out.println ("I am the construction method"); -     } the      -      Public Static voidMain (string[] args) { -Boke b=NewBoke (); -     } +      -}

The results of the operation are as follows:

It can be clearly seen in the creation of Boke this object, not only boke the construction method of this class executes, and constructs the code block also executes, more notice is constructs the code block execution before constructs the method, therefore has the following conclusion:

(1) The execution time of the construction code block is when the object is created, and automatic execution does not need to be called

(2) The execution priority of the construction code block is higher than the construction method

So it's very reasonable to call it Construction code block .

3. Static code block

Next we look at the third form of the Java code block, in fact, this form and construction code block very similar, but add a static modifier, the code is as follows (12-15 lines ):

1  Public classBoke {2     //method Body3      Public voidsay () {4System.out.println ("I'm the content of the code block."));5     }6     7     //common Java code blocks8     {9System.out.println ("I am an ordinary Java code block");Ten     } One      A     //Static code block -     Static{ -System.out.println ("I am a static Java code block"); the     } -      -      PublicBoke () { -System.out.println ("I am the construction method"); +     } -      +      Public Static voidMain (string[] args) { ABoke b=NewBoke (); at     } -      -}

Of course, what we said earlier is very similar to the "looks", and the time and frequency of execution are different. Consistent with the static properties or static methods we meet, this static block of code belongs to the class, and it is not like building blocks of code, every time a new object is created, it executes once when it is loaded into the JVM for the first time, and then no longer executes, verifying that there is a picture of the truth , modify the above code slightly, as follows:

1  Public classBoke {2     //method Body3      Public voidsay () {4System.out.println ("I'm the content of the code block."));5     }6     7     //common Java code blocks8     {9System.out.println ("I am an ordinary Java code block");Ten     } One      A     //Static code block -     Static{ -System.out.println ("I am a static Java code block"); the     } -      -      PublicBoke () { -System.out.println ("I am the construction method"); +     } -      +      Public Static voidMain (string[] args) { ABoke b=NewBoke (); at         Boke b1=new  Boke ();  -     } -      -}

Compared with the original change is not big, just add 23 lines, The new Boke object B1 is created. We all know that when we create a Boke object in line 22nd, the Boke class is already loaded into the JVM, and if the static code block should be executed only once, as we have previously deduced, is this the result of the run?

I marked the location of static code blocks with green grass, only once, stating that our assumptions are correct, but one thing we need to note is that the static method that we usually meet is not automatically executed (except for the Main method), and this is true automatically, so the person thinks that the static code block is called Class static initialization code block seems more appropriate, but the name is too long but not easy to remember, so it is called static code block it!

4. Synchronizing code blocks

The last one, often encountered in multi-threading, is the form: synchronized (obj) {}, because this form is similar to the method body, there is no special need to explain, not to repeat

Understanding of Java code blocks

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.