简体   繁体   English

从方法初始化的Java最终实例变量

[英]Java final instance variable initialized from a method

I need to make a final string in a class and initialize it from within a method, i searched the internet some says its possible some say not! 我需要在一个类中创建一个最终字符串,并从一个方法中对其进行初始化,我在互联网上搜索了一些表示可能的结果,有些则说不!
i passed over this and in the accepted answer he stated that's possible from constructor said nothing about non-constructor methods 我忽略了这一点 ,在接受的答案中,他说这是可能的,因为构造函数对非构造函数的方法一无所知

But, if you remove static, you are allowed to do this: 但是,如果删除静态,则可以执行以下操作:

class A  {    
    private final int x;

    public A()
    {
        x = 5;
    } }

in my case in Android i want to do this 就我而言,在Android中,我想这样做

public class MyActivity extends Activity
{
    final String DOWNLOADS_FOLDER_PATH;
    @Override
    public void onCreate(Bundle savedInstanceState)
    { ....

   DOWNLOADS_FOLDER_PATH=Environment.getExternalStorageDirectory().getPath()+"/downloads/";
    // i cant do that neither
    // DOWNLOADS_FOLDER_PATH="s";
    }
}

I need to initialize from a method because i have a call to 我需要从一个方法初始化,因为我有一个调用

Environment.getExternalStorageDirectory().getPath()

any idea? 任何想法?

Why cant you initialize it immediately? 为什么不能立即初始化它? getExternalStorageDirectory is static method, so: getExternalStorageDirectory是静态方法,因此:

static final String DOWNLOADS_FOLDER_PATH = Environment.getExternalStorageDirectory().getPath() + "/downloads/";

You can't initialize final variables from a method - only constructors and initializer blocks. 您无法通过方法初始化最终变量-只能使用构造函数和初始化程序块。 The reason for this is that the variable MUST NOT change its value throughout the lifetime of the object it belongs to (the meaning behind the 'final' keyword). 这样做的原因是,变量在其所属对象的整个生命周期中都不得更改其值(“ final”关键字的含义)。

Let's analyze the final variable's values over the course of the object creation and usage if you could initialize it with a normal method: 如果可以使用常规方法初始化变量,请在对象创建和使用过程中分析其最终变量的值:

  1. Object is created, final member's value is not initialized, so null; 对象已创建,最终成员的值未初始化,因此为null;
  2. Constructor returns, the object is used by the rest of the code. 构造函数返回,其余代码使用该对象。 The final variable is also used. 还使用了最终变量。
  3. You call a method that assigns a value to the final member. 您调用一个为最终成员分配值的方法。 The final value effectively changes. 最终值有效地改变。

You can use constructor as well 您也可以使用构造函数

public class MyActivity extends Activity
{
    final String DOWNLOADS_FOLDER_PATH;

    // constructor
    MyActivity(){
       super();
       DOWNLOADS_FOLDER_PATH=Environment.getExternalStorageDirectory().getPath()+"/downloads/";
    } 

    @Override
    public void onCreate(Bundle savedInstanceState)
    { ....

    }
}

From JLS: 从JLS:

A final variable may only be assigned to once. 最终变量只能分配一次。

But here if you assign final variable a value in any method other than constructor then it might get re-assigned. 但是在这里,如果您用构造函数以外的任何方法为最终变量赋值,则可能会重新赋值。 That is why assignation of final variable is not allowed in methods except constructor. 这就是为什么除构造函数外的方法中不允许分配最终变量的原因。

Constructor ensures that assignation is done for every new final variable. 构造函数确保为每个新的最终变量完成分配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM