This,static,super and Finalkeyword and code blocks in Java

Source: Internet
Author: User


This

    1. be able to use this represents a property in a class------this.name=name
    2. The ability to use this to emphasize the invocation of a method of this class
    3. The ability to use this to invoke the constructor of this class ------This (); Call the no-participation method in this class
    4. be able to use this represents the current object (the object that invokes the method)----The most important use

Static:

    1. Declaring a property---------property is a global variable
    2. Declaring methods---------Person.setname ("Tom"); Calling the static method with the class name

Final
    1. A class that uses final declaration cannot have child classes
    2. Methods that use Final declaration cannot overwrite a quilt class
    3. A variable that uses final declaration becomes a constant and cannot be changed (all uppercase letters are required when using final declaration variables)

Spuer:

    1. Calling the parent class's constructor method
    2. Calling the normal method of the parent class
    3. Calling the properties of the parent class

Note: This and super must be placed in the first line within the constructor method, formally for this reason, this () and super () cannot exist at the same time within a constructor method



Common block of code: a block of code defined directly in a method or statement

public class Codedemo01{public static void Main (String args[]) {{//plain code block int x = 30;//is a local variable System.out.println ("Normal code block -X = "+ X";} int x = 100;//the same System.out.println as the local variable name ("Outside the code block--X =" + x);}};


Building Blocks:code blocks that are written directly in the class

Class Demo{{//writes a block of code directly in the class, called the building Block System.out.println ("1, building block. ") ;} Public Demo () {//define construction Method System.out.println ("2, constructor method. ") ;}}; public class Codedemo02{public static void Main (String args[]) {new demo ();//Instantiate Object new Demo ();//Instantiate Object new Demo ();//Instantiate to Like}};

Execution Result:

1, building blocks.

2, the construction method.

1, building blocks.

2, the construction method.

1, building blocks.

2, the construction method.



Static code block: using Statickeyword declaration the code block

Class Demo{{//writes a block of code directly in the class, called the building Block System.out.println ("1, building block. ") ;} static{//uses static, called Static code block System.out.println ("0, Static code block");} Public Demo () {//define construction Method System.out.println ("2, constructor method. ") ;}}; Public class codedemo03{static{//defines the static block System.out.println ("code block defined in the class where the Main method resides") in the class where the main method resides;} public static void Main (String args[]) {new demo ();//Instantiate Object new Demo ();//Instantiate Object new Demo ();//Instantiate Object}};


Execution Result:

Blocks of code defined in the class where the Main method resides

0. Static code block

1, building blocks.

2, the construction method.

1, building blocks.

2, the construction method.

1, building blocks.

2, the construction method.


Conclusion:
    1. Static code blocks take precedence over the Main method run
    2. Static code blocks defined in a class take precedence over building blocks (no matter how many objects are generated, run only once)


This,static,super and Finalkeyword and code blocks in Java

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.