Definitions of Java constants

Source: Internet
Author: User

One, define constants in the interface:

Because the fields declared in Java interface are automatically added to the static final modifier at compile time, they are declared as constants.

Public interface constants{String name = "111";  } public class read{public static void Main (string[] args) {System.out.println ("class Constants ' s name ="      + Constants.name); }  }

Because a static final field is referenced, the compiler has compiled the contents of name interface constants into class read instead of referencing it. So only the changes in the name of class Read,interface constants are recompiled to be reflected in class read.

II. definitions in a class

public class constants{private static final String name = "111";      public static String GetName () {return name;  }} public class read{public static void Main (string[] args) {System.out.println ("class Constants ' s name      = "+ Constants.getname ()); }  }

Class Read is a reference to the GetName () method of the Constants class, and when the value of the constant name changes, we only need to modify and recompile the constants in class constants without compiling class read.

We can use the Anti-compilation tool provided by the JDK Javap decompile Read.class to see exactly, input: Javap-c read can see the difference between the two ways.

This article is from the Java My Favorites blog, so be sure to keep this source http://lindianli.blog.51cto.com/7129432/1703799

Definitions of Java constants

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.