Hanshunping to Java No. 08. Class variables

Source: Internet
Author: User

8.1 This

this-Precautions:

This cannot be used outside of the class definition and can only be used in methods defined by the class.

8.2 Class Variable 8.2.1 What is a class variable

A class variable (also known as a static variable) is a variable shared by all objects of that class, and any object of that class accesses it with the same value, and the same variable is modified when any object of that class modifies it.

8.2.2 How to define class variables

Definition syntax:

Access modifier static data type variable name;

For example, the static int total=0;

8.2.3 How to access class variables

Class name. class variable name or object name. Class variable Name

A class variable can be accessed by any object of the class, or it can be accessed by a class, but the member variables of the class are accessible only to the object and cannot be accessed by the class.

Summary of 8.2.4 class variables

1. When to use class variables

Case: Define student class, how much does the student pay?

Use a class variable that belongs to a public property.

2. Class variables differ from instance variables:

1) The addition of static is called a class variable or a static variable, otherwise called an instance variable.

2) class variables are public properties that are related to the class.

3) instance variables belong to each individual object's properties.

4) class variables can be accessed directly through the class name. class variable name.

Class 8.3 Methods

What is a class method and why is there a class method?

    • A class method belongs to all object instances in the following form:

Access modifier static data return type method name () {}

    • Note: A non-static variable (that is, a member variable) cannot be accessed in a class method, but a member method can access a static variable.
    • Use: Class name. class method name or object name. Class Method Name
    • Java rules: Class variables are used in principle to access and manipulate class methods.
Summary of methods of 8.3.1 class

1. When to use the class method

Case: Define student class, how much does the student pay?

1) class methods belong to class-related, public methods.

2) The instance method belongs to each individual object method.

3) class methods can be accessed directly through the class name. class method name.

Hanshunping to Java No. 08. Class variables

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.