Java constants and variables

Source: Internet
Author: User

Constant

A constant is an identifier whose value remains unchanged during running. Constants can only be referenced in the program, but cannot be assigned a value again.

Constant naming rules:

1. In JAVA, add the final keyword to the variable declaration to represent the constant, and add the static keyword to represent the class variable. In general, we combine static with final to declare a constant.

2. Use a meaningful constant to indicate the numbers or strings that will appear multiple times in the program.

Public static final double PI = 3.1415926;

Declare a variable in the Java interface. The public static final modifier is automatically added during compilation. Automatically declared as a global constant, so the Java interface is usually the best place to store constants.

Variable

Java variables are the most basic storage units in a program. Their elements include variable names, variable types, and scopes.

Every variable in a Java program belongs to a specific data type and must be declared before use. The declaration format is:

Type varName [= value] [{, varName [= value]}];
Int a = 10;
String B = "hello ";
Int d = 3, e, f = 5;

Type is one of the Java data types. The identifier is the name of the variable. Declare multiple variables of the specified type, separated by commas.

In essence, a variable is actually a small memory in the memory, and the variable name is used to access this area.

Therefore, each variable must be declared (declared) before use, and then assigned (filled with content) before use.

Local variables and member variables

Local variables: the variables defined in the method or statement block are called local variables.

Public void addData (){
Int a = 10; // The Variable "a" is a local variable. "a" cannot be used outside of the braces of the method.
}

Member variables: variables defined outside the method and inside the class are called member variables.

Public class TestAdd {
Static final int B = 10; // variable "B" is a member variable and can be used within the entire class.
}

Member variables can also be divided into class variables and instance variables.
Class variable: static variable. It can be called directly by class name or object, in addition, the same class variables of all objects share the same memory space. Static final variable, which must be initialized during declaration or in a static block
Initialization.

Instance variable: Without static modification, it can only be called through objects, and the same instance variable of all objects shares different memory space.

Note: If the name of a local variable in a method is the same as that of a global variable, the global variable is temporarily invalid in this method.


Variable scope

Variable scope refers to the part where the variable name in the program can be referenced. In this case, it may be abstract. The scope of a variable can be understood as the part in the program that can access the declared variable after the variable is declared.

Variable scopes are called the accessibility of variables. There are multiple rules in Java:

1. Variables defined in the statement block can only be accessed in the block. The scope of this variable is to declare its block

2. Variables defined in the internal block can access the variables defined in the external block.

3. Variables defined as method parameters can be accessed within the method body.

4. Class variables can be accessed anywhere in the class

5. If the internal block defines variables with the same name as the external block, the variables in the internal block overwrite the variables in the external block.

For example:

Public class Test {
Int a = 10;
Public void getData (){
Int B = 5;
For (int I = 0; I <B; I ++ ){
Int c = 9;
        }
    }
}

Variable a can be accessed anywhere inside the Test class, variable B can only be accessed within the method, and variable c can only work within the for loop.

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.