Common Code blocks in Java. Construct code blocks. Differences between static code blocks and code examples. java examples.

Source: Internet
Author: User

Common Code blocks in Java. Construct code blocks. Differences between static code blocks and code examples. java examples.

Execution sequence: (the priority ranges from high to low .) Static code block> mian method> constructor code block> constructor method.

The static code block is executed only once. The construction code block is executed every time an object is created.

1 common code block

1 // common code block: {} that appears in a method or statement is called a common code block. The execution sequence of common code blocks and general statements is determined by the order in which they appear in the code-"First show first run" 2 public class CodeBlock01 {3 public static void main (String [] args) {4 5 {6 int x = 3; 7 System. out. println ("1, variable x =" + x) in the normal code block; 8} 9 10 int x = 1; 11 System. out. println ("main method variable x =" + x); 12 13 {14 int y = 7; 15 System. out. println ("2, variable y =" + y in the normal code block); 16} 17} 18} 19 20/* 21 running result: 22 1, variable x in a common code block = 323 variable x in the main method = 124 2, variable y in the common code block = 725 */26

 

2. Construct a code block

// Constructor block: the code block defined directly in the class without the static keyword is called the {} constructor block. The constructor code block is called when an object is created. Each time an object is created, it is called. The execution order of the constructor code block is higher than that of the class constructor. Public class CodeBlock02 {System. out. println ("first code block");} public CodeBlock02 () {System. out. println ("constructor");} {System. out. println ("second constructed block");} public static void main (String [] args) {new CodeBlock02 (); new CodeBlock02 (); new CodeBlock02 ();}} /** execution result: the first code block, the second construction block constructor, the first code block, the second construction block constructor, the first code block, and the second construction block constructor */

 

3 static code block

// Static code block: A code block declared using the static keyword in java. The static block is used to initialize the class, which is the class attribute initialization. Each static code block is executed only once. Since the JVM executes static code blocks during class loading, the static code blocks are executed before the main method. // If the class contains multiple static code blocks, the code is executed according to "first defined code, then defined code and then executed ". // Note: 1 The static code block cannot exist in any method body. 2 static code blocks cannot directly access static instance variables and instance methods. They must be accessed through instance objects of the class. Class Code {System. out. println ("construction block of Code");} static {System. out. println ("static Code block of Code");} public Code () {System. out. println ("Code constructor") ;}} public class CodeBlock03 {System. out. println ("CodeBlock03 construction block");} static {System. out. println ("CodeBlock03 static code block");} public CodeBlock03 () {System. out. println ("CodeBlock03 constructor");} public static void main (String [] args) {System. out. println ("main method of CodeBlock03"); new Code (); new CodeBlock03 (); new CodeBlock03 ();}} /* CodeBlock03 static Code block CodeBlock03 main method Code static Code block Code construction method CodeBlock03 construction block CodeBlock03 construction method CodeBlock03 construction Method of block CodeBlock03

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.