Static and final keywords

Source: Internet
Author: User

Static

We use the static keyword in two special cases:

① we want to use a store to hold a specific data, no matter how many objects you want to create, or if you don't create the object at all.

② We need a special method that is not associated with any of the methods of the class.

public class Staticinjava {    static int i = N;    static void Statictest () {        staticinjava.i++;    }}    

Final

Before we talk about final, let's start by talking about the concept of "constant":

① compile-time constants, never change.

② the runtime Initializes a value, and we do not want this value to change.

Final data

For the base data type, final turns the value into a constant. For an object handle, final changes the handle to a constant, but the object that the handle points to is itself modifiable.

You can set the argument to the final property, and within this method, you cannot change the handle that the argument points to.

Final method

The inherited class cannot overwrite or overwrite this method by adding the final keyword to the method.

The compiler automatically replaces the method call with a copy of the actual code in the final method.

Final class

The final method for the class is not allowed to inherit.

Static and final keywords

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.