Static keyword and final keyword in Java

Source: Internet
Author: User

Static:

1. In a variable, the method is pre-decorated to represent static methods, static variables.

2.

Static {}

This form is a static block of code used to initialize a variable that is also modified by the final static. (Of course, the more common way is to assign values directly at the variable)

The contents of the static code block are run directly at the time the class is loaded

 Package Com.main; /**  */Publicclass  hellojava    {static{        System.out.println ("This line would be default initialized");    }      Public Static void Main (string[] args) {        System.out.println ("Hello World");}    }

The results of the operation are as follows:

This line would be default initialized
Hello World

Others such as static variables that are called directly with the class name are no longer detailed.

Final:

1. The uninitialized final member variable can be initialized only once in the constructor.

2. The uninitialized final local variable can be initialized only once in the owning code block.

Otherwise you will get an error.

Static keyword and final keyword in Java

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.