The difference between a static block of code in Java and a block of code construction

Source: Internet
Author: User

Source: https://liuyanzhao.com/5303.html

Blocks of code that are defined directly in the class and do not have the static keyword are called {} construction blocks of code.

construction blocks are called when the object is created, each time the object is created , and the order in which the code block is constructed takes precedence over the class constructor.

Static code block: a block of code declared in Java using the static keyword. static blocks are used to initialize the class and initialize the properties of the class. Each static block of code is executed only once. Because the JVM executes static blocks of code when the class is loaded, the static block of code executes before the main method .

Note: 1 Static code blocks cannot exist in any method body. 2 Static code blocks do not have direct access to static instance variables and instance methods and need to be accessed through instance objects of the class.

Execution order of static code blocks, construction code blocks, and constructors at the same time: static code blocks > constructs code blocks > Constructors ;

Let's give a few examples to illustrate everything.

Demo1: Construction method

  1. Public class Test {
  2. Static {
  3. System.out.println ("static Block");
  4. }
  5. {
  6. System.out.println ("building blocks, defined in class");
  7. }
  8. Public Test () {
  9. System.out.println ("construction Method Execution");
  10. }
  11. public static void Main (string[] args) {
  12. new Test ();
  13. new Test ();
  14. }
  15. }
  16. /*
  17. Static block
  18. Constructs a block, defined in a class
  19. Construction method Execution
  20. Constructs a block, defined in a class
  21. Construction method Execution
  22. */

Demo2: Normal code block

  1. /* Common code block: {} that appears in a method or statement is called a normal block of code.
  2. Ordinary blocks of code and general execution order of statements are determined by the order in which they appear in the code-"First Occurrence first" */
  3. Public class Test {
  4. public static void Main (string[] args) {
  5. {
  6. int x = 3;
  7. System.out.println ("1, variable x= within the normal code block" + x);
  8. }
  9. int x = 1;
  10. SYSTEM.OUT.PRINTLN ("variable x= within the Main method" + x);
  11. {
  12. int y = 7;
  13. System.out.println ("2, variable y= within the normal code block" + y);
  14. }
  15. }
  16. }
  17. /*
  18. 1, variable x=3 in normal code block
  19. Variable X=1 within the Main method
  20. 2, variable y=7 in normal code block
  21. */

DEMO3: Building Blocks of code

  1. /* Building blocks: Blocks of code that are defined directly in the class and do not have the static keyword are called {} construction blocks of code.
  2. Constructs a block of code that is called when the object is created, and is called every time the object is created.
  3. and the execution order of the Construction code block takes precedence over the class constructor. */
  4. Public class Test {
  5. {
  6. System.out.println ("first code block");
  7. }
  8. Public Test () {
  9. System.out.println ("construction Method");
  10. }
  11. {
  12. System.out.println ("second building block");
  13. }
  14. public static void Main (string[] args) {
  15. new Test ();
  16. new Test ();
  17. new Test ();
  18. }
  19. }
  20. /*
  21. First block of code
  22. Second building block
  23. Construction method
  24. First block of code
  25. Second building block
  26. Construction method
  27. First block of code
  28. Second building block
  29. Construction method
  30. */

Demo4: Static code block

  1. /* Static code block: a block of code declared in Java using the static keyword.
  2. Static blocks are used to initialize the class and initialize the properties of the class.
  3. Each static block of code is executed only once.
  4. Because the JVM executes static blocks of code when the class is loaded, the static block of code executes before the main method.
  5. If a class contains more than one static block of code, it will follow the code defined first, then execute after the definition.
  6. Note: 1 Static code blocks cannot exist in any method body.
  7. 2 Static code blocks do not have direct access to static instance variables and instance methods and need to be accessed through instance objects of the class. */
  8. Class Code {
  9. {
  10. System.out.println ("code building Block");
  11. }
  12. Static {
  13. System.out.println ("code static Block");
  14. }
  15. Public Code () {
  16. System.out.println ("code Method of Construction");
  17. }
  18. }
  19. Public class Test {
  20. {
  21. System.out.println ("Test's building Block");
  22. }
  23. Static {
  24. System.out.println ("Static code block for Test");
  25. }
  26. Public Test () {
  27. System.out.println ("test method of construction");
  28. }
  29. public static void Main (string[] args) {
  30. System.out.println ("The Main method of test");
  31. new Code ();
  32. new Code ();
  33. new Test ();
  34. new Test ();
  35. }
  36. }
  37. /*
  38. Static block of code for test
  39. The main method of test
  40. Code static block
  41. Code's building block
  42. How code is constructed
  43. Code's building block
  44. How code is constructed
  45. Test's construction block
  46. How to construct test
  47. Test's construction block
  48. How to construct test
  49. */

The difference between a static block of code in Java and a block of code construction

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.