Where to use the final modifier in Java

Source: Internet
Author: User
Tags constant final modifier variable
For the final variable in Java, the Java compiler is optimized. Every place that uses the final type variable is not accessed through a connection. For example, the test class uses a final int digit fnumber=77 in the data class, at which point the Java compiler compiles the 77 constant to the test class's instruction code or constant pool. This way, every time the test class uses fnumber, it is not connected to the data class by reference, but rather uses the copy that is saved in the class file directly.

To speak with a program:

C:\>javac Test.javaTest.java:
public class test{
public static void Main (string[] args) {
System.out.println (Data.fnumber);
}
}

Data.java:

public class data{
public static final int fnumber=77;
}
To execute commands and results:

C:\>javac Test.javac:\>javac Test.java

C:\>java Test
77

C:\>

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.