Static code blocks & non-static code blocks & constructors

Source: Internet
Author: User

Summary: static code blocks are always executed first. A non-static block of code, like a non-static method, is related to an object. Only non-static blocks of code are executed before the constructor. A non-static block of code and a constructor for a subclass are only started when the parent class is non-static and the constructor finishes executing (equivalent to the initialization of the parent class object). 

The same point: when the JVM loads the class and executes before the construction method executes, you can define more than one in the class, typically assigning some static variables to the code block.

Different points: Static code blocks are executed before non-static blocks of code (static code blocks, non-static code blocks, and construction methods).

A static code block executes only once for the first time, and then no longer executes, rather than a static block of code that executes once per new. A non-static block of code can be defined in a common method (though not very useful), while a static block of code does not. The JVM executes these static blocks of code when the class is loaded, and if there are multiple static blocks of code, the JVM executes them sequentially in the order in which they appear in the class, and each block is executed only once.

Example1:

    1. Public class Putong {
    2. Public Putong () {
    3. System.out.print ("default constructor Method!  --");
    4. }
    5. //non-static code block
    6. {
    7. System.out.print ("Non-static code block!  --");
    8. }
    9. //Static code block
    10. static{
    11. System.out.print ("Static code block!  --");
    12. }
    13. public static void Test () {
    14. {
    15. System.out.println ("code blocks in the normal Method!")  ");
    16. }
    17. }
    18. }
    19. Test class
    20. Public class TestClass {
    21. /** 
    22. * Distinguish between two times new static and non-static blocks of code execution
    23. */
    24. public static void Main (string[] args) {
    25. Putong C1 = new Putong ();
    26. C1.test ();
    27. Putong C2 = new Putong ();
    28. C2.test ();
    29. }
    30. }
    31. /*
    32. The results of the run output are:
    33. Static code block! --Non-static code block! --default constructor Method! --code blocks in the normal method!
    34. Non-static code block! --default constructor Method! --code blocks in the normal method!
    35. */ 

Example2:

  1. Package tags;
  2. Public class child extends father{
  3. Static {
  4. System.out.println ("child-->static");
  5. }
  6. private int n = 20;
  7. {
  8. System.out.println ("Child non-static");
  9. n = 30;
  10. }
  11. public int x = 200;
  12. Public child () {
  13. This ("The other constructor");
  14. System.out.println ("Child constructor Body:" + N);
  15. }
  16. Public child (String s) {
  17. System.out.println (s);
  18. }
  19. public void Age () {
  20. System.out.println ("age=" + N);
  21. }
  22. public void Printx () {
  23. System.out.println ("x=" + x);
  24. }
  25. public static void Main (string[] args) {
  26. new Child (). Printx ();
  27. }
  28. }
  29. Class Father {
  30. Static {
  31. //system.out.println ("n+" +n);
  32. //When n is defined below, you will be prompted cannot reference a field before it is defined,
  33. //So the n definition must be moved above to output
  34. System.out.println ("super-->static");
  35. }
  36. public static int n = 10;
  37. public int x = 100;
  38. Public Father () {
  39. System.out.println ("Super ' s x=" + x);
  40. Age ();
  41. }
  42. {
  43. System.out.println ("Father non-static");
  44. }
  45. public void Age () {
  46. System.out.println ("nothing");
  47. }
  48. }

Results:

Super-->static

Child-->static

Father non-static

Super ' s x=100

Age=0

Child non-static

The other constructor

Child constructor Body:30

x=200

Parent class static code block, subclass static code block, parent class non-static code block-parent class constructor-subclass non-static code block-subclass constructor

In Java, when you create an instance object of a class using the new operator, you begin to allocate space and initialize the member variable to the default value, note that this does not mean that the variable is initialized to the initial values at the variable definition, but instead assigns a value of 0 to the shape and assigns null to the string, which is different from C + +. (Student.name = null, Student.age = 0) and then enters the constructor of the class. Inside the constructor, the first thing to check for this or super call is to complete the call between the constructors of the class itself, and the super call is to complete the call to the parent class. The two can only appear in one, and can only appear as the first sentence of the constructor. Implement the program's jump when calling this and super, instead of executing the called this constructor or super constructor. At the end of this and super, the program performs the initialization of the variables at the time of the class definition. This completes the execution of the remaining code in the constructor.

Static code blocks & non-static code blocks & constructors

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.