最终变量与静态最终变量之间的差异 [英] Difference between final variables vs static final variables

查看:142
本文介绍了最终变量与静态最终变量之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚研究了最终数据成员,我认为最终变量与静态最终变量的区别是什么?

I was just studying about final data members and i thought what would be the difference final variables vs static final variables ??

据我所知,静态和最终的字段只有一个存储空间,而最终变量的存储空间与每个实例相关联。

I understand that a field that is both static and final has only one piece of storage, and final variable will have storages associated with every instance.

但即使我只声明一个变量final,然后它对所有对象保持不变,因为我需要在程序本身而不是在运行时初始化它们。

But even if i declare a variable only final, then it remains the same for all the objects as i need to initialize them in the program itself and not at the run time.

所以,除了与内存相关的问题外,基本上两者没有区别?

So, basically there is no difference between the two except for the memory related issue ??

推荐答案


但即使我声明一个变量只是final,那么它仍然是相同的
所有对象,因为我需要在程序本身初始化它们
而不是在运行时。

But even if i declare a variable only final, then it remains the same for all the objects as i need to initialize them in the program itself and not at the run time.

否,非静态最终成员可以在构造函数中初始化。之后不能重新分配。

No, non-static final members can be initialized in the constructor. They cannot be re-assigned after that.

这篇关于最终变量与静态最终变量之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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