Common code blocks in Java, building blocks of code, static code block differences, and code examples

Source: Internet
Author: User
Tags instance method

Common code blocks in Java, building blocks of code, static code block differences, and code examples

The static code block and the execution of the constructor sequence, has been very confused, read Sun Weichen teacher's "Java Object-oriented programming" and the teacher's Java basic Video in the explanation, now use the following small procedure to brief the narrative, in order to deepen understanding, and June mutual encouragement ....

public class Fu {

static {
System.out.print (1);

}

Public Fu () {
System.out.print (2);
}
}

public class Zi extends Fu {

                                         Static {
                                               System.out.print ("a");
                                                   }

Public Zi () {
System.out.print ("B");
}
}

public class Demo {

public static void Main (string[] args) {
Fu fu = new Zi ();
Fu = new Zi ();
}
}

1.
when both the parent and child classes have static code blocks and constructors, the order of execution is as follows:

Parent class static code block > Subclass Static code block when the Java Virtual machine loads the class, it executes the block code.

Parent class Constructors > Subclass Constructors (Father first, child only)

If it is a multilevel inheritance, the parent class of the upper layer executes first, then decrements

Summary: Static first execution, parent class takes precedence over subclass execution
Static code blocks are executed when the JVM loads the class, and the static code block executes and executes only once


2.

When you call a method in a class, the member variables in the class are assigned first before the method body executes, and the variable defaults are given if the code does not have a specific value assigned to it. The order in which the member variables are assigned is in order. If the code has both a direct assignment and a constructor assignment, then it is executed according to the order of precedence.

3.
Override (Override) overload (overload)
Overloads, in a class definition, you can write several methods of the same name, but as long as they have different signature parameter lists, Java will consider them as the only method. In a nutshell, a method in one class has the same name as another, but the parameter table is called an overloaded method rewrite, and the subclass implements the method in the parent class again;
1. Two methods that occur method override return value, method name, parameter list must be exactly the same
2. Subclass throws an exception that cannot exceed the exception thrown by the corresponding method of the parent class
3. The access level of the subclass method cannot be lower than the access level of the corresponding method of the parent class (public,package,protected, Private)

4. Dynamic binding idea: In a run-time environment, when a series of instance methods (including one method call another method) are called through Subclass B, the precedence is dynamically bound to the instance methods contained in class B itself, and if Class B does not define this instance method, it is dynamically bound to an instance method inherited from the parent class. Read the above instructions, we should also know the above program output results.
The output is: 1a2b2b

Execution order: (Priority from high to low.) Static code block >mian method > construct code block > Construct method.

Where static code blocks are executed only once. The construction code block executes every time the object is created.

1 Common code blocks

1//Common code block: {} that appears in a method or statement is called a normal code block. Ordinary blocks of code and general execution order of statements are determined by the order in which they appear in the code--"first occurs first" 2 public class codeblock01{3 public       static void Main (string[] args) {4            5             {6               int x=3; 7               System.out.println ("1, variable x= in normal code block" +x);     8             } 9             int x=1;11             System.out.println ("variable x= within the Main method" +x); {+                int y=7;15                System.out.println ("2, variable y= in normal code block" +y);             }17           }18     }19     /*21     operation Result:     1, the variable in the normal code block x=323 the          variable x=124 in the Main method          2, Variable y=725 in normal code block     */26     

2 Building Code Blocks

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. 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. public class codeblock02{    {      System.out.println ("first code block");        }        Public CodeBlock02 () {        System.out.println ("constructor method");        }                {          System.out.println ("second building Block");        }      public static void Main (string[] args) {          new CodeBlock02 ();          New CodeBlock02 ();          New CodeBlock02 ();               }}    /** Execution Result: first code block second construction block construction method first code block second construction block construction method first code block construction method of the second construction block */

3 Static code block

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. If a class contains more than one static block of code, it will follow the code defined first, then execute after the definition. 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.    Class Code{{System.out.println ("Code building Block");        } static{System.out.println ("code static Block");        } public code () {System.out.println ("code construction Method");         }} public class codeblock03{{System.out.println ("CODEBLOCK03 building Block");        } static{System.out.println ("CodeBlock03 Static code block");            } public CodeBlock03 () {System.out.println ("construction Method of CodeBlock03");            } public static void Main (string[] args) {System.out.println ("Main method of CodeBlock03");            New Code ();            New Code ();            New CodeBlock03 ();          New CodeBlock03 (); }}/*codeblock03 Static code block CodeBlock03 The main method code static code blocks code construction block Code construction Method Code construction block code construction Method CODEBLOCK03 building block CodebThe construction method of the construction block CodeBlock03 of the Lock03 method CodeBlock03 */ 

Common code blocks in Java, building blocks of code, static code block differences, and code examples

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.