Static code blocks, which distinguish between building blocks of code and constructors

Source: Internet
Author: User
Tags constructor

First, static blocks of code, construction blocks, and constructors all exist in a class, except that they perform in different order and execution times.

A static block of code that executes only once, for example, when you want to create 10 objects, execute once when you create the first object, and then when you continue to create the remaining objects, the static code block is not executed.

and the similarities and differences between the code block and the constructor: the same point: Each object is created, the construction code block and the constructor are executed once, different points: the order of execution is different, the construction code block is executed first, and then the construction method is executed.

First, demonstrate the execution of static code blocks in code

Class Staticcode

{

Static

{

System.out.println ("I am a static code block");

}

void Del ()

{

System.out.println ("Execution");

}

}

Class demo{

public static void Main (string[] args)

{

for (int i=0;i<=1;i++)

New Staticcode (). del ();

}

}

Output Result:

I am a static block of code

Perform

Perform

To construct code blocks and constructors to execute the procedure:


Class ant{
{
System.out.println ("I am Building code block 1");
}
Ant ()
{
Run ();
}
void Run ()
{
System.out.println ("I am a constructor");
}

{
System.out.println ("I am Building code block 2");
}

}
public class ASD {


public static void Main (string[] args) {
TODO auto-generated Method Stub
for (int i=0;i<1;i++)
New Ant ();
}


}

Execution Result:

I'm building code block 1
I'm building code block 2
I'm a constructor.


The following is a personal understanding:

After reading the difference between the three, it is estimated that there will be doubt, the reason why, here to do a more vivid analogy, a class, like some of the abstract collection of similar objects.

11 Main method is a treasure

21 is an object.

For example, there are such a group of people to find the treasure, when a person to find the door to the treasure, open it into the (this is equivalent to a static code block), and the latter people do not need to open the door of the treasure, because the door opened, each in the equivalent of creating an object.



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.