I'm learning about constructors in an online MOOC and I have an original piece of code as below:
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);
}
}
Now I'm required to change all variables to private and add constructors for the fields name, number and balance.
I'm creating two constructors as below:
public Account(double newBalance){balance = newBalance;}
public Account(String newName, int newNumber, double newBalance){
this(newBalance);
name = newName;
number = newNumber;
}
Adding these constructors have resulted in error on the final variable declaration as below:
Variable mOut might not have been initialized.
The errors go away when I remove my two new constructors or I remove final from the variable mOut.
May I know why this error is occurring? I've tried looking through StackOverflow for answers but can't find similar situation to mine. Thank you.