Java Variable Type __java

Source: Internet
Author: User

Types of variables in Java


The Java language variable types are: Local variables, member variables, class variables three, in the Java language, all variables must be declared before use.

Local variables

Definition: A local variable is declared in a method, construction method, or statement block.

Attention matters

1. Local variables are created when methods, construction methods, or statement blocks are executed, and when they are executed, the variables are destroyed.

2, access modifiers cannot be used for local variables.

3. Local variables are visible only in the method, constructor, or statement block in which they are declared.

4, local variables are distributed on the stack.

5. Local variables have no default values, so local variables are declared and must be initialized before they can be used.


Member variable

Definition: Also known as instance variables, declared in a class, but outside methods, construction methods, and statement blocks.

Attention matters

1. When an object is instantiated, the value of each instance variable is then determined.

2. The instance variable is created when the object is created and destroyed when the object is destroyed.

3. The value of an instance variable should be referenced by at least one method, construct method, or statement block, allowing external access to instance variable information in these ways.

4, the instance variable can be declared before use or after use.

5, access modifiers can be decorated with instance variables.

6. Instance variables are visible to methods, constructs, or statement blocks in a class, and you should generally set the instance variable to private by using the access modifier to make the instance variable visible to the subclass.

7, the instance variable has a default value, the default value of the numeric variable is 0, the default value of the Boolean variable is false, the default value for the reference type variable is NULL, the value of the variable can be specified at declaration time, or it can be specified in the constructor.

8. Instance variables can be accessed directly through variable names, but in static methods and other classes, you should use the fully qualified name: Obejectreference.variablename.

class variables

Definition: Also called a static variable, declared in a static keyword in a class, but must be outside the method construction method and the statement block.

Attention matters

1. No matter how many objects a class creates, the class has only one copy of the class variable.

2. Static variables are rarely used in addition to being declared as constants. Constants are variables declared as public/private,final and static types that cannot be changed after a constant initialization.

3. Static variables are stored in a static storage area, often declared as constants, and rarely use static declaration variables alone.

4. Static variables are created at the beginning of the program and destroyed at the end of the program.

5. Similar visibility to instance variables, but most static variables are declared public by order to be visible to the consumers of the class.

6, the default value and instance variables are similar, the default value for a numeric variable is 0, the Boolean default is False, the reference type default is NULL, the value of the variable can be specified at declaration time, or it can be specified in the constructor, and static variables can also be initialized in static statement blocks.

7, static variables can be accessed through: Classname.variablename way.

8. When a class variable is declared as a public static final type, the class variable name must be in uppercase letters, and if the static variable is not public and final, it is named in the same way as the instance variable and the local variable.


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.