The difference between interface and class in a constant defined in Java

Source: Internet
Author: User

The final conclusion: defining constants in both interface and class is actually OK, the key is to look at your design and personal interests.

In Java, the variables defined in interface are "public static final" types, which are constants, so many people define common constants in interface.

Here are two ways to define constants that are functionally equivalent:

Class

Package Com.example;public Final class Constants {    private Constants () {}    int ;     int  - ;     int  - ;     int  - ;}

Interface

Package Com.example;public interface Constants {    int;     int  - ;     int  - ;     int  - ;}

Class definition constants and interface definition constants:

1, class definition constants, need to be defined as final and define a private method of construction, so that other classes are not inherited, prohibit instantiation of this class, called directly in the "class. Constant" way.

2, the "variable" defined in the interface is actually a constant, and the "variable" in the interface is the "public static final" type by default, which is the constant, so the interface can omit "public static final" and write "type variable" directly.

3, with the above class definition constants, can not be instantiated and can not be inherited, looks perfect.

4, interface definition constants, although not instantiated, it can be implemented by other classes;

So there is a design pattern "The Constant interface pattern". The so-called "constant interface pattern", that is, the other class to use the constants defined in the interface, the implementation of the interface. I think this is a bad use of the interface. The constants defined in the interface should be constants that are frequently used by all classes, but not every class uses all the constants defined in the interface, importing unnecessary constants into the class, and the subclasses of the class will also base the imported constants, which can lead to confusion and should be avoided.

5. The same constants are defined in interface and class, and the class file generated by interface is smaller and more concise and more efficient than the class file generated by class. You can use Jad and JAVAP decompile to analyze it.

Personal Summary:

1, do not use "Constant interface mode", this mode will cause the constant confusion in the class, increase maintenance difficulty. Attention! is not used in a constant interface mode, but can be implemented using interfaces for common designs.

2, do not use static import, import static * * * * * * * *, because import static will cause maintainability degradation, the maintenance of the person looking at the code, not so clear or not so quickly know which file the constant. It is recommended to use constants where the "interface. Constant Name" method is used.

3. It is also useful to define constants in this way: Public abstract class Constants {}, it is easy to see the purpose of defining constants in this way: disallow instantiation. However, the purpose of "abstract class" is to allow other classes to inherit, so the semantic awkward, also has the definition of interface defined constants, that can be inherited, in fact, the more graceful way to solve the above problems is to use the final modification of the class and to define a private construction method.

4, for use is to define the constant with interface or use class to define the constant, see a person likes. The interface definition constants are more graceful: The code is more concise, the resulting class file is smaller, and the JVM does not consider the additional features of the class, such as method overloading, and thus more efficient. This is an anti-pattern usage, many people do not like this usage, if we know its advantages and disadvantages, extension of short, it is understandable, there is one thing is not to use this usage as "constant interface mode", personally think "constant interface mode" is really a kind of interface "pool use."

Reference:

http://blog.csdn.net/voo00oov/article/details/50433672 (the above content is transferred from this article)

Http://my.oschina.net/gschen/blog/134794?fromerr=zKrZIUb5

Http://stackoverflow.com/questions/2659593/what-is-the-use-of-interface-constants

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

http://blog.csdn.net/pan_junbiao/article/details/5906745

Definitions of constants in Java in interface and Class (RPM)

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.