Methods and recommendations for defining constants in Java (Class/interface)

Source: Internet
Author: User
Tags modifier

class defines a constant method (recommended method)
//final修饰符public final class Constants { //私有构造方法 private Constants() {} public static final int ConstantA = 100; public static final int ConstantB = 100; ......}

Called with the class. Constant name method. You need to privatize the construction method to avoid creating instances of the class. There is no need for other classes to inherit the class at the same time.

If you need to access the constants defined in the tool class in multiple places, you can avoid using the class name to modify the constant name by using the static import mechanism.

Interface defining a constant method
public interface Constants {    int ConstantA = 100; int ConstantB = 100; ......}

The field declared in interface, the virtual machine is automatically added to the public static final modifier at compile time. The use method is typically "interface. Constant name". You can also directly access the constant name, which is the constant interface pattern, by implementing the interface.

constant Interface : That is, the interface contains no methods, contains only static final fields, and each field exports a constant. Classes that use these constants implement this interface to avoid using the class name to decorate the constant name.

The constant interface pattern is a bad use of the interface. Specific references are as follows:

The constant interface pattern is a poor use of interfaces. That a class uses some constants internally are an implementation detail. Implementing a constant interface causes this implementation detail to leak into the class ' s exported API. It is of no consequence to the users of a class that the class implements a constant interface. In fact, it may even confuse them. Worse, it represents a commitment:if in a future release the class was modified so it's no longer needs to use the cons Tants, it still must implement the interface to ensure binary compatibility. If a nonfinal class implements a constant interface, all of its subclasses'll has their namespaces polluted by the cons Tants in the interface.

There is several constant interfaces in the Java platform libraries, such as java.io.ObjectStreamConstants. These interfaces should be regarded as anomalies and should is emulated.

Difference

Comparing the two methods, the class file generated by the method defined in interface is smaller than the class file of the first method, and the code is more concise and more efficient.

In Java, however, there are problems, mainly Java dynamics, in which some fields in Java can be dynamically referenced at run time. In some scenarios, partial content changes can only be partially compiled. Specific example Reference document is Java Interface the best place for constant storage?

It is recommended that you use class to define constants, but use the private modifier to get the constants through the Get method. This scheme guarantees the dynamic nature of Java.

public class A{    private static final String name = "bright"; public static String getName(){ return name; }

Reference:

Https://www.ibm.com/developerworks/cn/java/l-java-interface/index.html

Https://www.jianshu.com/p/0affad4762ef

Methods and recommendations for defining constants in Java (Class/interface)

Related Article

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.