Java member variables, local variables, static variables, class variables, non-static variables, instance variables, forward references, illegal forward references, static code blocks, non-static code blocks

Source: Internet
Author: User
Tags ming


There are two types of member variables for the ①java class:         A variable that is modified by the static keyword, called a class variable or static variable           Another no static modifier, for member variables      ② popular Point said:       The static variables of the    class are only one in memory, and the Java Virtual machine allocates memory for static variables in the process of loading the class, and the static variable is in the method area and is shared by all instances of the class. Static variables can be accessed directly from the class name, depending on the life cycle of the class.          The instance variable depends on the instance of the class. Each time an instance is created, the Java Virtual machine allocates memory for the instance variable once, and the instance variable is in the heap, whose life cycle depends on the life cycle of the instance.   NOTE: The order of initialization in     1.java:         load class;          static variable initialization;         static block; " It can only dispatch static, cannot dispatch non-static "         member variable;          construction method;       2. To show the priority of a static block, I'm going to show a program without the main method, javac it without any errors. public class Test {    static {         System.out.println ("Static");    &Nbsp;    system.exit (0);    }} 

Variables in Java are broadly divided into two broad categories: member variables and local variables.

member variables:
Variables defined within the class body are called member variables;
If the member variable has a static keyword decoration, the member variable is calledStatic VariablesOrclass Variables
If the member variable does not have a static keyword decoration, the member variable is callednon-static variablesOrinstance Variable

Local Variables:
A parameter, a variable defined within a method, a variable defined in a code block, belong to a local variable.

class variables ( static variables )
1. Can forward reference
2. Variables belong to the class itself
3. Class variables do not depend on instances of classes, class variables are only allocated once in the stack memory at initialization time, no matter how many instances of the class are created, no longer allocate space for class variables
4. Access class variables through any instance of the class, and the underlying will turn them into class variables through the class itself, with the same effect
5. Once the value of the class variable is changed, the class variable is accessed through any instance of the class or class, and the resulting value is changed.
6. The class will be initialized before the initialization of the

instance variable (non-static variable)
1. Cannot forward reference, if forward reference, is called illegal forward reference, this is not allowed
2. The variable belongs to the instance object of the class
3. Allocating memory space as instances of classes are created

non-static code block
Code that is wrapped directly by {}, called a non-static block of code

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

Initialization time for class variables (static variables), instance variables (non-static variables), static code blocks, non-static blocks of code
Modified by the static keyword (such as: class variables [static variables], static code blocks) will be initialized before the class is initialized to create an instance object, and is executed sequentially from top to bottom;
Initialization without the static keyword adornments (e.g., instance variables [non-static variables], non-static blocks of code) is actually extracted into the constructor of the class, but will be executed in a class constructor
Code blocks are executed first, and they are executed sequentially from top to bottom.

Static final variables must be initialized at the time of declaration or in a static static block.
So if you want to use program input as his variable, you must modify the variable type
You can write
public class xx
{
     public final String PackageName;
     public xx (String name)
     {
         PackageName = name;
     }
}
Define a static instance of xx public static xx xxInstance = new xx ("entered value");
When called, write xxInstance.PackageName



There is never a difference between a member variable and a local variable.
We are like a class is a company,
The member variable is the official employee of this company, always exists, with the company in the same life and extinguished.
and local variables are temporary workers, the company asked temporary workers to do a little thing, finished, temporary workers have no, was destroyed! (Good Supernatural)

It is true that a member variable is called a global variable. It's all in this class.

In fact, there is a larger than the member variable ... Maybe you don't understand, but you'll know later.
I build a common class, I declare a static variable inside, then I can use it anywhere. It is also popularly called < global variables > class : An object with the same attributes is called a class, which is an abstract concept. For example: Cars, people, dogs, God; Object : Everything in everyday life is an object and an instantiation of a class. For example, a bulldozer is an example of a car; Yao Ming is a human instantiation; the small white (dog's name) is the instantiation of the dog, and the second lang God is the example of God; attribute : A property that is used to describe the characteristics of a particular object and is static. For example: Yao Ming is more than 2.6 meters tall, small white hair is brown, two lang God has an eye on the forehead; methods : Each object has its own behavior or method of using them, such as a dog will run will be called and so on, we call these behavior methods, is dynamic, you can use these methods to manipulate an object; members of a class: properties and methods are called members of this object, because they are the main part of an object, and without these two things, the object has no meaning.






-The above is my personal notes after the completion of the PDF and understanding and insight, not necessarily hundred pairs, the following attached I test sample code, will be more persuasive

Sample code


1
2/**
3 *-----------------------------------------
4 * @ file: Statical.java
5 * @ Author: fancy
6 * @ Email: [Email protected]
7 * @ Time: 2012-7-9
8 * Description: TEST
9 *-----------------------------------------
10 */
11public class Statical{
12
13/**
14 * Static code block
15 * Class variables (static variables) can be referenced forward (i.e., referenced first, then defined)
16 */
Static{
The name = "Fancydeepin"; The definition of name after use
SYSTEM.OUT.PRINTLN ("---> Static code block is executed <---");
20}
21st/**
22 * Class variables (static variables) are initialized before the initialization of a class, regardless of how many instances of the class are created
23 * Class variables (static variables) will only allocate a space on the stack memory at the time of initialization
24 * All static modifiers will be executed sequentially, so,
The value of * name finally outputs fancy instead of the above Fancydeepin
26 */
public static String name = "Fancy"; class variables (static variables)
-Private String mail = "Myemail"; Instance variable (non-static variable), which is defined when the initial value is specified, and is executed sooner than the values given in the constructor
29
Public statical (){
mail = "[email protected]";
SYSTEM.OUT.PRINTLN ("---> Constructor code block is executed <---");
33}
34/**
35 * Non-static code block
36 * In fact, a non-static block of code is extracted into the constructor of the class when the class is initialized to create the instance.
37 * However, a non-static block of code will take precedence over the code block in the constructor
38 * So, mail finally output is the value given in the class constructor, that is [email protected]
39 * Instead of [email protected], not myemail
40 */
$ {
protected mail = "[email]";
SYSTEM.OUT.PRINTLN ("---> Non-static blocks of code are executed <---");
44}
45
//Getting and setting
-


Java member variables, local variables, static variables, class variables, non-static variables, instance variables, forward references, illegal forward references, static code blocks, non-static code blocks


Related Article

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.