为其他变量创建构造函数时Java最终变量声明出错

为其他变量创建构造函数时Java最终变量声明出错,java,constructor,Java,Constructor,我正在在线MOOC中学习构造函数,我有一段原始代码,如下所示: package com.example.accountapp.logic; import com.example.accountapp.ui.OutputInterface; /** * This file defines the Account class. It provides the basis for a * series of improvements you'll need to make as you pr

我正在在线MOOC中学习构造函数,我有一段原始代码,如下所示:

package com.example.accountapp.logic;

import com.example.accountapp.ui.OutputInterface;

/**
 * This file defines the Account class.  It provides the basis for a
 * series of improvements you'll need to make as you progress through
 * the lessons in Module 6.
 */
public class Account {
/**
 * This is the variable that stores our OutputInterface instance.
 * <p/>
 * This is how we will interact with the User Interface
 * [MainActivity.java].
 * </p>
 * This was renamed to 'mOut' from 'out', as it is in the video
 * lessons, to better match Android/Java naming guidelines.
 */
final OutputInterface mOut;

/**
 * Name of the account holder.
 */
String name;

/**
 * Number of the account.
 */
int number;

/**
 * Current balance in the account.
 */
double balance;

/**
 * Constructor initializes the field
 */
public Account(OutputInterface out) {
    mOut = out;
}

/**
 * Deposit @a amount into the account.
 */
public void deposit(double amount) {
    balance += amount;
}

/**
 * Withdraw @a amount from the account.  Prints "Insufficient
 * Funds" if there's not enough money in the account.
 */
public void withdrawal(double amount) {
    if (balance > amount)
        balance -= amount;
    else 
        mOut.println("Insufficient Funds");
}

/**
 * Display the current @a amount in the account.
 */
public void displayBalance() {
    mOut.println("The balance on account " 
                 + number
                 + " is " 
                 + balance);
}
}
public Account(double newBalance){balance = newBalance;}

public Account(String newName, int newNumber, double newBalance){
  this(newBalance);
  name = newName;
  number = newNumber;
}
添加这些构造函数导致最终变量声明出现错误,如下所示:

package com.example.accountapp.logic;

import com.example.accountapp.ui.OutputInterface;

/**
 * This file defines the Account class.  It provides the basis for a
 * series of improvements you'll need to make as you progress through
 * the lessons in Module 6.
 */
public class Account {
/**
 * This is the variable that stores our OutputInterface instance.
 * <p/>
 * This is how we will interact with the User Interface
 * [MainActivity.java].
 * </p>
 * This was renamed to 'mOut' from 'out', as it is in the video
 * lessons, to better match Android/Java naming guidelines.
 */
final OutputInterface mOut;

/**
 * Name of the account holder.
 */
String name;

/**
 * Number of the account.
 */
int number;

/**
 * Current balance in the account.
 */
double balance;

/**
 * Constructor initializes the field
 */
public Account(OutputInterface out) {
    mOut = out;
}

/**
 * Deposit @a amount into the account.
 */
public void deposit(double amount) {
    balance += amount;
}

/**
 * Withdraw @a amount from the account.  Prints "Insufficient
 * Funds" if there's not enough money in the account.
 */
public void withdrawal(double amount) {
    if (balance > amount)
        balance -= amount;
    else 
        mOut.println("Insufficient Funds");
}

/**
 * Display the current @a amount in the account.
 */
public void displayBalance() {
    mOut.println("The balance on account " 
                 + number
                 + " is " 
                 + balance);
}
}
public Account(double newBalance){balance = newBalance;}

public Account(String newName, int newNumber, double newBalance){
  this(newBalance);
  name = newName;
  number = newNumber;
}
变量mOut可能尚未初始化

当我删除两个新构造函数或从变量mOut中删除final时,错误就会消失


我可以知道为什么会发生这个错误吗?我试图通过StackOverflow查找答案,但找不到与我类似的情况。多谢各位

对于最后一个变量mOut,无论何时创建对象,它都应该有一个值

在另一个构造函数中使用所需的默认值初始化mOut


请参阅:

在java中,您必须在声明或构造函数中初始化最终变量。
如果最终变量未在声明yy中初始化,则无法创建未初始化它的构造函数。

因为变量被声明为
final
,因此它们必须在对象创建过程中初始化。稍后,您不应该修改声明为final的引用来引用任何其他对象(或原语变量的任何其他值)。因此,您需要初始化所有构造函数中的所有最终变量。当您定义构造函数时,如果假设您有2个构造函数(重载),则意味着您有两种方法来创建对象。在这种情况下,每种方法都必须初始化最终变量


Edit注意定义为
final
的变量不能保证不变性。只是您不能重新分配它们(使用
=
),它们只能且必须初始化一次(在您的所有构造函数中,在声明时使用局部变量)。当然,您仍然可以自由使用
(点运算符),如果有任何变异方法,您可以调用它们。

这是因为并非所有构造函数都将初始化
mOut
。如果有人调用一个新构造函数,则不会设置
mOut
。这种情况有什么不清楚的地方?我不知道为什么其他变量(所有变量都必须在每个构造函数中声明)的情况不是这样,但只有final类型。谢谢,非常清楚的解释。我没有意识到最后一个变量的这种行为。