Java “静态最终整数”是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25383863/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 00:17:26  来源:igfitidea点击:

What does "static final int" mean?

javafinal

提问by Tomer8009

I'm new to java and the book surprisingly started using this without explaining it even once. Why does stack overflow want me to write even more than I actually need?

我是 Java 新手,这本书出人意料地开始使用它,甚至一次都没有解释。为什么堆栈溢出要我写的比实际需要的还要多?

采纳答案by Epiglottal Axolotl

staticmeans that instead of each instance of the class having that variable, the variable belongs to the class as a whole.

static意味着不是类的每个实例都具有该变量,而是该变量作为一个整体属于该类。

finalmeans that the values are constant and cannot be changed.

final意味着这些值是恒定的,不能改变。

Basically what this means is that it's an integer that is constant for all instances of a certain class at all times.

基本上这意味着它是一个整数,对于某个类的所有实例始终都是常量。

回答by TheLostMind

static --> the field is at class level, not at instance level. i.e, you can access the field using MyClass.myField.

static --> 该字段在类级别,而不是在实例级别。即,您可以使用 MyClass.myField 访问该字段。

final --> for primitives --> value can't change

final --> 对于基元 --> 值不能改变

for non-primitives --> you can't change the reference and point it to another object. Note : It is different from immutability.

对于非原语 --> 您不能更改引用并将其指向另一个对象。注意:它与不变性不同。

回答by amar abdulla

static - denotes class level member and memory is allocated only once, all objects will have access to the same memory reference

static - 表示类级成员,内存只分配一次,所有对象都可以访问相同的内存引用

final - denotes a constant variable, value cannot be changed at anytime.

final - 表示一个常量变量,值不能在任何时候改变。

you can declare

你可以声明

class Login 
{
    public static final int USER = 1;   
    // We are not able to change the value during execution 
    public static final int ADMIN = 2;
}