为什么默认情况下接口变量是 static 和 final 的? [英] Why are interface variables static and final by default?

查看:24
本文介绍了为什么默认情况下接口变量是 static 和 final 的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么 Java 中的接口变量默认是 static 和 final 的?

Why are interface variables static and final by default in Java?

推荐答案

来自 Philip Shaw 的 Java 界面设计常见问题解答:

From the Java interface design FAQ by Philip Shaw:

接口变量是静态的,因为 Java 接口本身不能被实例化;变量的值必须在不存在实例的静态上下文中分配.final 修饰符确保分配给接口变量的值是一个真正的常量,不能由程序代码重新分配.

Interface variables are static because Java interfaces cannot be instantiated in their own right; the value of the variable must be assigned in a static context in which no instance exists. The final modifier ensures the value assigned to the interface variable is a true constant that cannot be re-assigned by program code.

来源

这篇关于为什么默认情况下接口变量是 static 和 final 的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆