[Java] Static, final

Source: Internet
Author: User

1. Static members

Static members are independent of the class's objects, and exist before the existence of the object. Static members have only one instance space, regardless of how many objects of a class are created. A static variable is shared by all objects of the same class. When you change the static variable of one of the objects, the values of the static variables of the remaining objects change accordingly, because they share the same amount in the same space.

Because static variables are independent of specific objects, static variables are defined and can be invoked in the following two ways:

Object name. Static variable Name

Class name. Static variable Name

2. Static methods

Static methods are commonly defined in the form of:

public static int out () {

Method body

}

Static methods use:

The name of the object. Static method Name

Class name. static method Name

3.final

Final class

Some classes are well-defined when writing a program. For consistency or security reasons, you do not want to be inherited by another class, you should decorate the class as the final class. The final class cannot be inherited.

Public final class test{

Class Body

}

In Java, many common classes are final types, such as String strings, math classes, math, and so on.

Final member

Final-Modified member variables are called constants

The final decorated member method is called the final method, and the resulting method cannot be overridden in the derived subclass of the current class. The final method defines the form:

Public final void out () {

Method body

}

[Java] Static, final

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.