java构造函数赋值,Java构造函数最终变量赋值

public class User

{

private final String _first_name;

private final String _last_name;

private final String _org_ID;

private final TimeZone _time_zone;

private final InternetAddress _email;

private final Date _last_login;

private final Date _creation_date;

public User( final String org_ID,

final String username,

final String first_name,

final String last_name,

final List roles,

final TimeZone time_zone,

final InternetAddress email,

final Date last_login,

final Date creation_date )

{

this( null, org_ID, username, first_name, last_name, roles, time_zone );

this._email = email;

this._last_login = last_login;

this._creation_date = creation_date;

}

The compiler gives the the following error for the 3 variable assignments respectively:

"variable _email might already have been assigned"

Is the compiler unable to tell the variables are not set in the call to the first ctor? What am I missing here?

解决方案

To add on to what irreputable said, you may want to refactor your code so that the constructor with fewer arguments calls the constructor with more arguments, specifying a reasonable default or null, as the case may warrant.

The reason you're getting the error is because the constructor with fewer arguments has to deal with the fields that aren't explicitly dealt with. However, if you flip the way the constructors are called, you will avoid this issue.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值