Java Fundamentals-Variable constants

Source: Internet
Author: User
Tags modifier

Tag: type run initial static mode information in case of static keyword member

Variable

A small area in memory that requires a variable name to access
Name of variable: variable type variable name = Variable Value example: String stuname= "Wangwei";
All punctuation in Java is in English.
Rules for using variables
You can only assign one value at a time by declaring a post-initialization or declaring it at the same time.
Modify the use of multiple variables using String name; Name=wangwei; or String name= "Wangwei";
When assigning a variable, use single quotation marks for char type, double quotation marks for string

Local variables

A local variable is declared in a method, a construction method, or a block of statements;
Local variables are created when methods, construction methods, or block of statements are executed, and when they are executed, the variables are destroyed;
Access modifiers cannot be used for local variables;
A local variable is only visible in the method, construction method, or block of statements that declares it;
Local variables are allocated on the stack.
The local variable has no default value, so the local variable is declared and must be initialized before it can be used.

Member variables (instance variables)

Instance variables are declared in a class, but are stored in the heap outside of methods, construction methods, and statement blocks;
When an object is instantiated, the value of each instance variable is then determined;
Instance variables are created when the object is created and destroyed when the object is destroyed;
The value of the instance variable should be referenced by at least one method, construct method, or statement block, so that the external can obtain instance variable information through these methods;
Instance variables can be declared before use or after use;
An access modifier can modify an instance variable;
Instance variables are visible to methods, construction methods, or block statements in a class. In general, you should set the instance variable to private. By using the access modifier, the instance variable is visible to the child class;
The instance variable has a default value. The default value for a numeric variable is 0, the default value for a Boolean variable is false, and the default value for the reference type variable is null. The value of a variable can be specified at the time of declaration, or it can be specified in a constructor method;
Instance variables can be accessed directly from the variable name. However, in static methods and other classes, you should use the fully qualified name: Obejectreference.variablename.
Attention:
Member variables: defined in a class to describe what the object will be used in the class
Local variables: defined in a method of a class, temporarily saved in a method using the current method
Different scopes, all methods in a class can use member variables, and local variables are used only in the current method
Attention:
1java will give the member variable an initial value does not give the local variable the initial value
2 Local variables with the same name are not allowed in the same method
3 When the authority variable and member variable have the same name, the local variable is used preferentially

Static variables (class variables)

Class variables, also known as static variables, are declared in the class with the static keyword, but must be outside the method construction method and statement block.
No matter how many objects a class creates, the class has only one copy of the class variable.
Static variables are seldom used except when declared as constants. Constants are variables declared as public/private,final and static types. Constants cannot be changed after initialization.
Static variables are stored in a static storage area. are often declared as constants, and static declaration variables are seldom used alone.
Static variables are created at the beginning of the program and are destroyed at the end of the program.
has similar visibility to instance variables. However, in order to be visible to the consumer of a class, most static variables are declared as public types.
The default value is similar to the instance variable. The default value for numeric variables is 0, the Boolean default is False, and the reference type default value is null. The value of a variable can be specified at the time of declaration, or it can be specified in a constructor method. In addition, static variables can be initialized in static statement blocks.
Static variables can be accessed by: Classname.variablename.
Class variable names are generally recommended to use uppercase when the class variable is declared as public static final type. If the static variable is not public and final, it is named in the same way as the instance variable and the local variable.
Attention:
Static variables can be accessed through the class name, or through object access
Static methods (class methods) can be accessed through the class name or through the object.
1) A non-static member cannot be called directly to create an object of a class in a method that accesses a non-static constant through an object
2) Ordinary member methods can directly access the same non-static variables and static variables
3) Non-static methods cannot be called directly in static methods, and non-static methods need to be accessed through objects
Static initialization blocks:
1) When the program runs, static initialization is executed first, then normal initialization is performed, and the construction method is executed at the end of the execution
2) Static methods are only executed when the class is loaded and executed only once, while static initialization can only assign values to static variables and cannot initialize normal member variables

Differences between static and instance variables in Java

Static variables belong to a class that does not produce objects and can call static variables through the class name.
An instance variable belongs to an object of that class, and the class object must be produced to invoke an instance variable.
Differences in program run time:
An instance variable is a property of an object, and an instance object must be created where the instance variable is allocated space before the instance variable can be used.
Static variables are not part of an instance object, but belong to a class, so also known as class variables, as long as the program loads the class's bytecode, without creating any instance objects, static variables will be allocated space, static variables can be used.

Constant

Special variables It's value is not allowed to change during program run variable name general capitalization
Final String love= "Love"
When a constant is used, the prefix 0 represents 8, and the prefix 0x represents the 16 binary, for example:
int decimal = 100;
int octal = 0144;
int hexa = 0x64;
String constants
char a = ' \u0001 ';
String a = "\u0001";

Java Fundamentals-Variable constants

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.