-2

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.

yoges nsamy
  • 1,275
  • 1
  • 16
  • 29

3 Answers3

1

As the variables are declared as final hence they must be initilized during object creation itself. Later you are not supposed to modify the references declared as final to refer to any other object (or any other value for primitive variables). Hence you need to initilize all your final variables in all your constructors. When you define your constructors and if suppose you have 2 constructors (overloaded) implies you have two ways to create your object. In such a case each way must initilize the Final variables.

Edit Note variables defined as final does not gurantee immutability. Its just that you can not reassign them (using =), they can and must only be initilized once (In your case in all your constructors, in case of local variables at the time of declaration itself). Of course you are still free to use . (dot operator) and if there are any mutator methods you can invoke them.

nits.kk
  • 5,204
  • 4
  • 33
  • 55
0

For the final variable mOut, It should have a value whenever an object is created.

Initialise mOut in the other constructor with the default value you want

Refer : How final keyword works

Community
  • 1
  • 1
Stenal P Jolly
  • 737
  • 9
  • 20
0

In java you have to initiliaze final variable on declaration or in constructor. And if final variable is not initialized on declaration yyou can't create constructor which doesn't initialize it.

Jay Smith
  • 2,331
  • 3
  • 16
  • 27