java 何时选择变量声明为最终静态

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13578668/
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-10-31 13:17:13  来源:igfitidea点击:

When to choose variables to declare as final static

javaandroidstaticfinal

提问by Sahil Mahajan Mj

I have used final and static variables as well. what i found about these variables is,

我也使用了 final 和 static 变量。我对这些变量的发现是,

final variable

最终变量

  • A final variable can only be initialized once, either via an initializer or an assignment statement.

  • Unlike the value of a constant, the value of a final variable is not necessarily known at compile time.

  • final 变量只能通过初始化程序或赋值语句初始化一次。

  • 与常量的值不同,最终变量的值在编译时不一定是已知的。

what variables should i declare as final-

我应该将哪些变量声明为 final-

Most often i use those variables whose value is constant universally and can never changed, such as the value of PI.

大多数情况下,我使用那些值普遍不变且永远不会改变的变量,例如 PI 的值。

public static final double PI = 3.141592653589793;

static variables

静态变量

  • These are those variables which belongs to the class and not to object(instance).

  • Static variables are initialized only once , at the start of the execution .

  • A single copy to be shared by all instances of the class

  • A static variable can be accessed directly by the class name and doesn't need any object.

  • 这些是属于类而不是对象(实例)的变量。

  • 静态变量仅在执行开始时初始化一次。

  • 由类的所有实例共享的单个副本

  • 静态变量可以通过类名直接访问,不需要任何对象。

what variables should i declare as final-

我应该将哪些变量声明为 final-

Most of the time, i use those variables which i want to initialize only once and use them in the enitre class.

大多数时候,我使用那些我只想初始化一次的变量,并在整个类中使用它们。

When to use final static variable

何时使用最终静态变量

Now, i came across a term final staticin one of my database project. I found that some of the database objects or even database version were declared as statci final.

现在,我final static在我的一个数据库项目中遇到了一个术语。我发现一些数据库对象甚至数据库版本被声明为 statci final。

 static final String DATA_BASE = "BackUpDatabase.db";
    static final int DATA_BASE_VERSION = 1;

Now, my question is what variables should we declare as finalor staticor final static, as using either of them could have solved the issue, then wyh to use both together.

现在,我的问题是什么变量,我们应该声明为finalstatic或者final static,如使用其中一方可能已经解决了这个问题,那么WYH同时使用起来。

回答by Harsha_2012

static -  Only use when a variable which is used globally 
final -  Only use when you need to declare a value as constant 

static final - Only use when a value is globally used and it is a constant.

: - Here global means across all the instances of a java class

回答by FThompson

Variables declared as static final(or vice versa) are understood to be meaningful constants, and are named in all upper-case with underscores for spaces.

声明为static final(或反之亦然)的变量被理解为有意义的常量,并以所有大写字母命名,空格下划线。

An example of a commonly encountered constant is Integer.MAX_VALUE, or Math.PI.

常见常量的一个示例是Integer.MAX_VALUE, 或Math.PI

回答by rai.skumar

finalonly says that value once initialized can't be changed; staticsays that the attribute belongs to Class and NOT objects.

final只说初始化后的值不能改变;static表示该属性属于 Class 而不是对象。

So when you say final static; this means there is just one copy of variable and it can't be changed.

所以当你说final static 时;这意味着只有一份变量副本并且无法更改。

回答by Kumar Vivek Mitra

-staticin java means Class's member. Its shared by all the instances of the class.

-static在 java 中表示 Class 的成员。它由类的所有实例共享。

-finalkeyword in java means, constant, but has different interpretation depending on what its being applied.

-finalJava 中的关键字表示常量,但根据其应用的不同而有不同的解释。

-When we use static finalon a field, consider it as a Global variable.

-当我们static final在一个字段上使用时,将其视为一个全局变量。

-PIis static variable of Math Class and its directly accessed using the class name, as Math.PI.

-PI是 Math Class 的静态变量,使用类名直接访问它,如Math.PI.

-Use all letters in caps to define a static final variable.

-使用大写中的所有字母来定义静态最终变量。

final's interpretation:

最终解释:

final variable :Its value canNot be changed

final variable :它的值不能改变

final method :It canNot be overridden

final method :它不能被覆盖

final class :It canNot be extended

final class :不能延长

final Parameter :Its value canNot be changed which it receives from caller's argument

final Parameter :它的值不能改变,它从调用者的参数接收

final Object Reference Variable :It canNot refer to any other object, other than the one its currently referring to

final Object Reference Variable :它不能引用任何其他对象,除了它当前引用的对象

回答by Heggi

Declaring variables only as static can lead to change in their values by one or more instances of a class in which it is declared.

仅将变量声明为静态变量会导致声明变量的类的一个或多个实例更改其值。

Declaring them as static final will help you to create a CONSTANT as @Vulcan told. Only one copy exists which can be accessed anywhere.

将它们声明为 static final 将帮助您创建一个 CONSTANT,正如@Vulcan 所说。只有一个副本存在,可以在任何地方访问。

回答by user6440666

Static Variable

静态变量

  • You can change the static variable value by calling static method present in same class .
  • Static variable value will be same for all object created from this class . if we change the value then all object of that class will get new value ,old value will be lost.
  • Value can be changed multiple times.
  • 您可以通过调用同一个类中存在的静态方法来更改静态变量值。
  • 对于从此类创建的所有对象,静态变量值将相同。如果我们更改该值,则该类的所有对象都将获得新值,旧值将丢失。
  • 值可以多次更改。

Final variable

最终变量

This variable value can be initialized by two way:

这个变量值可以通过两种方式初始化:

  1. At the time of declaring the variable.
  2. At the time of creating object of that class where class constructor will have this.finalvariable = newfinalvariablevalue;
  1. 在声明变量时。
  2. 在创建该类的对象时,类构造函数将具有 this.finalvariable = newfinalvariablevalue;

Once initialized it can't be changed by any method (Static or non-static).

一旦初始化,它就不能被任何方法(静态或非静态)更改。

回答by Emre Kilinc Arslan

static methods or classes are implicitly final . Because there is only one copy of this method existing for all the objects which means that subclasses dont have access to modify the copy. lets say you have a method in the parent and you dont want subclasses to change this method.. just declare the parent method as a final. Here you go.

静态方法或类是隐式 final 的。因为所有对象只存在此方法的一个副本,这意味着子类无权修改副本。假设您在父类中有一个方法,并且您不希望子类更改此方法.. 只需将父方法声明为 final。干得好。

class Parent {

    final void myMethod() {
        //No one can change this method from subclassess 
        //compiler works efficiently because it knows that this method will not change
    }
}

class Child extends Parent{
   //from this class I can use myMethod but I cannot override.
}