Java member variables, local variables, static variables,

Source: Internet
Author: User

Fancydeepin preface: This is the basic knowledge of java. You can skip it, and the code can also be refined smoothly. This was a long time ago, what I think when I started from C ++ to java, I would stick to my own philosophy:
Focus on practice, application, and theory. Of course, there will be different views on the same thing at different stages. This idea is not suitable for me at a certain time and has long been abandoned. Go to the topic.

Variables in java are roughly divided into member variables and local variables.

Member variables:
The variables defined in the class body are called member variables;
If the member variable is modified with the static keyword, the member variable is called a static variable or a class variable;
If the member variable is not modified with the static keyword, the member variable is called a non-static variable or an instance variable.

Local variables:
Parameters, variables defined in the method, and variables defined in the Code block are all local variables.

Class variable (static variable)
1. can be referenced forward
2. Variables belong to the class itself
3. class variables do not depend on class instances. class variables are allocated only once in the stack memory during initialization. No space is allocated for class variables no matter how many times the class instances are created.
4. Use any instance of the class to implement class variables. The underlying layer converts the variables into class variables through the class itself, and their effects are the same.
5. Once the value of a class variable is changed, you can use any instance of the class or class to renew the class variable.
6. Will be initialized before class initialization

Instance variable (non-static variable)
1. Forward references cannot be made. If forward references are made, they are called illegal forward references. This is not allowed.
2. The variable belongs to the Instance Object of the class.
3. allocate memory space as the class instance is created

Non-static code block
Code directly wrapped up by {}, called a non-static code block

Static code block
Code directly wrapped by static {}, called a static code block

Class variable (static variable), instance variable (non-static variable), static code block, non-static code block initialization time
Modified by the static keyword (for example, class variables [static variables] and static code blocks) will be initialized before the class is initialized to create an instance object, it is executed sequentially from top to bottom;
Initialization without static keywords (for example, instance variables [non-static variables] and non-static code blocks) will be extracted to the class constructor for execution, however
The code block is executed first, and is executed sequentially from top to bottom.

-The above are my personal notes, understanding, and opinions after I have reviewed the PDF file. I don't have hundreds of thousands of pairs. The following is an example of my testing code, which will be more informative.

Sample Code

1
2 /**
3 *-----------------------------------------
4 * @ file: Statical. java
5 * @ Author: fancy
6 * @ mail: fancydeepin@yeah.net
7 * @ time: 2012-7-9
8 * @ Description: TEST
9 *-----------------------------------------
10 */
11 public class Statical {
12
13 /**
14 * Static code block
15 * class variables (static variables) can be referenced forward (I .e., referenced first and then defined)
16 */
17 static {
18 name = "fancydeepin"; // name is defined after use
19 System. out. println ("---> the static code block is executed <---");
20}
21 /**
22 * class variables (static variables) are initialized before class initialization, regardless of how many instances will be created for the class.
23 * all class variables (static variables) will be allocated only once in the stack memory during initialization.
24 * All static modifiers are executed in order by location. Therefore,
The value of 25 * name finally outputs fancy instead of the above fancydeepin.
26 */
27 public static String name = "fancy"; // class variable (static variable)
28 private String mail = "myEmail"; // instance variable (non-static variable). The initial value is specified during definition and will be executed earlier than the value assigned by the constructor.
29
30 public Statical (){
31 mail = www.2cto.com;
32 System. out. println ("---> the constructor code block is executed <---");
33}
34 /**
35 * non-static code blocks
36 * In fact, non-static code blocks will be extracted to the class constructor during class initialization and creation,
37 * However, non-static code blocks are executed preferentially than the code blocks in the constructor.
38 * therefore, mail finally outputs the value specified in the class constructor, that is, www.2cto.com.
39 * instead of www.2cto.com, not myEmail
40 */
41 {
42 mail = "www.2cto.com ";
43 System. out. println ("---> the non-static code block is executed <---");
44}
45
46 // getting and setting
47

Sample Code of the test class

1
2 /**
3 * class variables (static variables) are initialized before the class is initialized and created.
4 */
5 System. out. println ("-----------------> @ 1 <----------------");
6 System. out. println ("name --->" + Statical. name); // @ 1
7 System. out. println ("-----------------> @ 1 <----------------");
8 /**
9 * create a class instance object
10 */
11 System. out. println ("-----------------> @ 6 <----------------");
12 Statical statical = new Statical (); // @ 6
13 System. out. println ("-----------------> @ 6 <----------------");
14 /**
15 * instance-based callback class variables. The underlying layer will be converted to callback class variables through the class itself.
16 */
17 System. out. println ("-----------------> @ 2 <----------------");
18 System. out. println ("name --->" + statical. name); // @ 2
19 System. out. println ("-----------------> @ 2 <----------------");
20 /**
21 * if the value of the class variable is changed, then the class variable will get the changed value.
22 */
23 Statical. name = "fancydeepin ";
24
25 System. out. println ("-----------------> @ 3 <----------------");
26 System. out. println ("name --->" + statical. name); // @ 3
27 System. out. println ("-----------------> @ 3 <----------------");
28
29 System. out. println ("-----------------> @ 4 <----------------");
30 System. out. println ("name --->" + Statical. name); // @ 4
31 System. out. println ("-----------------> @ 4 <----------------");
32 /**
33 * timing when non-static code blocks and constructors are executed
34 */
35 System. out. println ("-----------------> @ 5 <----------------");
36 System. out. println ("mail --->" + statical. getMail (); // @ 5
37 System. out. println ("-----------------> @ 5 <----------------");
38

Background output result


-----------------> @ 1 <----------------
---> Static code block executed <---
Name ---> fancy
-----------------> @ 1 <----------------

-----------------> @ 6 <----------------
---> Non-static code block executed <---
---> The constructor code block is executed <---
-----------------> @ 6 <----------------

-----------------> @ 2 <----------------
Name ---> fancy
-----------------> @ 2 <----------------

-----------------> @ 3 <----------------
Name ---> fancydeepin
-----------------> @ 3 <----------------

-----------------> @ 4 <----------------
Name ---> fancydeepin
-----------------> @ 4 <----------------

-----------------> @ 5 <----------------
Mail ---> fancydeepin@yeah.net
-----------------> @ 5 <----------------

By fancydeepin

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.