为什么界面变量默认为静态和最终? [英] Why are interface variables static and final by default?

查看:82
本文介绍了为什么界面变量默认为静态和最终?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么接口变量在Java中默认为静态和最终?

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

推荐答案

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

From the Java interface design FAQ by Philip Shaw:


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

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.

source

这篇关于为什么界面变量默认为静态和最终?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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