在Java中,最终字段是否可以从构造函数帮助程序中初始化? [英] In Java, can a final field be initialized from a constructor helper?

查看:151
本文介绍了在Java中,最终字段是否可以从构造函数帮助程序中初始化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个最终的非静态成员:

  private final HashMap< String,String& myMap; 

我想使用构造函数调用的方法初始化它。因为myMap是final,所以我的helper方法不能直接初始化它。当然我有选项:



我可以直接在构造函数中实现myMap初始化代码。

  MyConstructor(String someThingNecessary)
{
myMap = new HashMap< String,String>();

myMap.put(blah,blahblah);
// etc ...

//与myMap无关的其他初始化程序
}

我可以使用我的帮助方法构建HashMap,将它返回到构造函数,然后让构造函数将对象分配给myMap。

  MyConstructor(String someThingNecessary)
{
myMap = InitializeMyMap(someThingNecessary);

//与myMap无关的其他初始化东西
}

private HashMap< String,String> InitializeMyMap(String someThingNecessary)
{
HashMap< String,String> initializedMap = new HashMap< String,String>();

initializedMap.put(blah,blahblah);
// etc ...

return initializedMap;
}

方法2很好,但是我想知道是否有一些方法我可以允许帮助方法直接操作myMap。也许一个修饰符,指示它只能被构造函数调用?

  MyConstructor(String someThingNecessary)
{
InitializeMyMap(someThingNecessary);

//与myMap无关的其他初始化函数
}


//帮助程序不工作,因为它无法修改最终成员
private void InitializeMyMap(String someThingNecessary)
{
myMap = new HashMap< String,String>();

myMap.put(blah,blahblah);
// etc ...
}


解决方案>

方法2是你最好的选择。问题是,如果你在一个私有方法中有一个赋值,那么没有什么东西阻止类中的其他代码调用它,这将创建一个问题,尝试第二次分配到最终字段。



Java没有一个只能在构建过程中调用的单独方法的构造。



为了完整起见,我们可以创建第三个选项,其中您在初始化时分配映射,然后使帮助方法填充它:

  private final HashMap   

然后:

  MyConstructor(String someThingNecessary)
{
initializeMyMap(someThingNecessary);

//与myMap无关的其他初始化函数
}


//帮助程序不工作,因为它无法修改最终成员
private void initializeMyMap(String someThingNecessary)
{

myMap.clear();
myMap.put(blah,blahblah);
// etc ...
}

混淆你可以使用初始化器而不是构造函数,但你不应该这样做,因此除非你真的需要知道,否则我不会扩展。


I have a final non-static member:

private final HashMap<String,String> myMap;

I would like to initialize it using a method called by the constructor. Since myMap is final, my "helper" method is unable to initialize it directly. Of course I have options:

I could implement the myMap initialization code directly in the constructor.

MyConstructor (String someThingNecessary)
{
    myMap = new HashMap<String,String>();

    myMap.put("blah","blahblah");
    // etc...

    // other initialization stuff unrelated to myMap
}

I could have my helper method build the HashMap, return it to the constructor, and have the constructor then assign the object to myMap.

MyConstructor (String someThingNecessary)
{
    myMap = InitializeMyMap(someThingNecessary);

    // other initialization stuff unrelated to myMap
}

private HashMap<String,String> InitializeMyMap(String someThingNecessary)
{
    HashMap<String,String> initializedMap = new HashMap<String,String>();

    initializedMap.put("blah","blahblah");
    // etc...

    return initializedMap;
}

Method #2 is fine, however, I'm wondering if there's some way I could allow the helper method to directly manipulate myMap. Perhaps a modifier that indicates it can only be called by the constructor?

MyConstructor (String someThingNecessary)
{
    InitializeMyMap(someThingNecessary);

    // other initialization stuff unrelated to myMap
}


// helper doesn't work since it can't modify a final member
private void InitializeMyMap(String someThingNecessary)
{
    myMap = new HashMap<String,String>();

    myMap.put("blah","blahblah");
    // etc...
}

解决方案

Method #2 is your best option. The problem is that if you have an assignment in a private method there is nothing preventing other code in the class outside the constructor calling it, which would then create an issue with an attempted second assignment to the final field.

Java has no construct of a separate method that can only be called during construction.

For completeness, we can make a third option, where you assign the map at initialization and then have the helper method fill it:

 private final HashMap<String, String> myMap = new HashMap<String, String();

And then:

 MyConstructor (String someThingNecessary)
 {
    initializeMyMap(someThingNecessary);

    // other initialization stuff unrelated to myMap
 }


 // helper doesn't work since it can't modify a final member
 private void initializeMyMap(String someThingNecessary)
 {

     myMap.clear();
    myMap.put("blah","blahblah");
    // etc...
  }

And if you really want to be confusing you can use an initializer instead of a constructor, but you should not do that, so unless you really need to know, I won't expand on that.

这篇关于在Java中,最终字段是否可以从构造函数帮助程序中初始化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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