JAVA basics/Lesson 11th: naming rules for variables/JAVA, JAVA scopes, and java Naming rules

Source: Internet
Author: User

JAVA basics/Lesson 11th: naming rules for variables/JAVA, JAVA scopes, and java Naming rules

I. Naming rules for JAVA

Variable naming must meet a series of conditions and cannot be customized.

1. Naming rules

Variable names can only contain letters and numbers $ _
The first character of a variable can only contain letters $ _
The first character of a variable cannot contain numbers.
Note: _ is an underline, not a minus sign or a broken number.

1 int a = 5; 2 int a_12 = 5; 3 int $ a43 = 5; 4 5 int a434 = 5; 6 7 // the first digit is a number, 8 int 34a = 5;
2. Use the full name, not the abbreviation.
Public class Hero {// use the full name, which is easy to understand: String name; float hp; float armor; int moveSpeed ;}
Public class Hero {// use the abbreviation, which is not easy to understand: String n; float h; float a; int m ;}
3. You cannot only use keywords, but can contain keywords.

What is a keyword?
For example, words such as int, short, and class have been given special meanings by java, and these are keywords. Variable names cannot be used directly.
For example, class
Int class = 5;
Class is a keyword
But it can contain
For example
Int class5 = 5;

public class HelloWorld {      public static void main(String[] args) {          int class5 = 5;             }}
4. keyword list

5. Chinese characters can also be used to name variables.

Chinese characters can also be used to name variables, but in practice... Don't do this...

Public class instructor {public void () {System. out. println ("");} public static void main (String [] args) {instructor Cang = new Cang (); Cang. carry out teaching work ();}}

Ii. JAVA Scope

Variables are in different locations and have different names.
They are
Field, attribute
Parameters
Local variable
Variables with different names have different scopes.

1. Field, attribute, Field

When a variable is declared under the class, it is called a Field or attribute, a member variable, and a Field such as variable I, it is an attribute.
The entire class can be accessed from the position where the variable declaration is located in Row 3.
Therefore, its scope is the entire class starting from its declared position.

Public class HelloWorld {int I = 1; int j = I; // other attributes can access I public void method1 () {System. out. println (I); // you can access I} public void method2 () {System. out. println (I); // method 2 can access I }}
2. Parameters

If a variable is declared on a method, it is called a parameter.
The parameter scope is all the code in the method.
Other methods cannot access this parameter.
Class cannot access this parameter either.

Public class HelloWorld {public void method1 (int I) {// The scope of parameter I is the method method1 System. out. println (I);} public void method2 () {System. out. println (I); // method2 cannot access the parameter I} int j = I; // The parameter I} cannot be accessed in the class}
3. Local Variables

A variable declared in a method is called a local variable. Its scope is at the beginning of the Declaration and at the end of the block.

Public class HelloWorld {public void method1 () {int I = 5; // Its function ranges from the declared 4th rows to the position of the 12 rows after the block is in the System. out. println (I); {// sub-block System. out. println (I); // you can access I int j = 6; System. out. println (j); // you can access j} System. out. println (j); // cannot access j, because its scope ends after 10th rows }}

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.