Java Learning Notes-fourth Chapter

Source: Internet
Author: User
Tags modifiers

Chapter fourth variables and constants

1. Access control modifiers for Java

Use the access control modifier to restrict access to data. There are 4 levels of access control modifiers: private, protected, public, and default (no modifier specified).

Type/Permissions Private Protected Public Default
accessible accessible accessible access to
other classes in the same package (including subclasses) inaccessible accessible accessible accessible
subclasses of different packages inaccessible accessible accessible < Span style= "FONT-SIZE:14PX;" > not accessible
Non-subclass in different packages Not accessible Not accessible Access to Not accessible

2. Variables: variables are the amount of changes that can occur during the course of a program's operation to temporarily store data while the program is running. To use a variable, you must first declare the variable. the variables in Java are divided into Local variables (variables in the body of the method) and member variables (variables outside the method body in the class).

declare the syntax format of a variable: data type variable name 1, variable name 2, ... ;

You can declare one variable at a time, or you can declare more than one variable at a time, separated by commas (for example, int A; or int a,b,c;). You can assign a value to a variable at the same time you declare it (for example, int a=10; or float x,y=5.23f;)

Local Variables: The scope of a local variable is limited to the body of the method in which it resides , beyond the bounds of the method body (that is, the method body's {}) is invalid and cannot be called. The system does not automatically initialize local variables, that is, local variables do not have default values .

member variables: member Variables Act on the entire class , and for member variables, you can add access control modifiers (such as public, private, and so on) to the word "static" to restrict access to variables and how variables are created. The system automatically initializes member variables that are not initialized, where the numeric variable is initialized to 0, the character variable is initialized to ' o ', the compound variable is initialized to NULL, and the Boolean variable is initialized to False.

static variable:Two types of member variables are included in Java. The static modifier, called a class variable (also called a static variable), is called an instance variable that is not modified by static. The difference : Class variables are shared across all objects of the class, that is, no matter how many objects are created, share a class variable for all objects in the class. Class variables can be accessed through the class name or an object. instance variables can only be accessed through objects, and each object accesses its own instance variables individually.

3. Constants: constants are quantities that cannot be changed during a program's operation and are used to store a fixed value. In Java, regular final is used to declare constants , and constant names are all capitalized , so that they differ from variable names. Constants also have member constants and method constants, which are similar in usage to features and variables.

Java Learning Notes-fourth Chapter

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.