Confusion as to initialization (or lack thereof) of object

package lab9;

import java.util.Scanner;
import java.util.Date;

public class AccountManager
{
    // the account
    private Account account;
    // reader for reading user input
    private Scanner reader;
    private Transaction transaction;
    private AccountManager accountmanager;



    /**
     * Constructor for objects of class AccountManager
     */
    public AccountManager(Account account, Transaction transaction, AccountManager accountmanager)
    {
        this.account = account;
        reader = new Scanner(System.in);
        this.transaction =transaction;
        this.accountmanager=accountmanager;




    }

    public void start()
    {
        System.out.println("WELCOME TO THE ACCOUNT MANAGER APPLICATION");
        boolean finished = false;

        for(int i=0; i<3; i++)
        {
            // get amount
            System.out.print("Transaction amount to add?\n>");
            String inputAmount = reader.nextLine();
            double amount = Double.parseDouble(inputAmount);

            // get type
            System.out.print("Transaction type?\n>");
            String type = reader.nextLine();

            // TO DO: get reference
            System.out.print ("Transaction reference?\n>");





            // date is today's date
            Date date = new Date();

            // TO DO: create transaction and add to account


            System.out.println("Transaction added");
            account.displayTransactions();

            // TO DO: display balance
        }
    }

    public static void main(String[] args)
    {

        Transaction t=new Transaction();

        Customer cust = new Customer("Fernando", "Alonso");
        Account account = new Account(cust, "12345");
        AccountManager accountmanager= new AccountManager (account,t, accountmanager);



        // TO DO: create AccountManager object and call start method

    }

}

Enclosed is my code. I am trying to create an AccountManager object and call its start method.

However, I am getting an error identifying that the Accountmanager object may not have been intialized.

I am confused by this greatly; I thought

this.accountmanager=accountmanager;
  1. Was the initialization of the accountmanager object? My (limited) understanding is that the initialization involves =.

such as

a=10;

And if that were true, then that to me suggests that this.accountmanager=accountmanager would be valid? (Which I am assuming is incorrect, but just wanted to provide my thinking process).

  1. I had previously gotten an error returned to me, identifying that a non-static variable transaction cannot be referenced from a static context.

To remedy this I included:

 Transaction t=new Transaction();

So that in fell in the scope of the static method. Is that legal/correct?

Error message:

 AccountManager accountmanager= new AccountManager (account, t, accountmanager);

"variable accountmanager might not have been initialized"

Jon Skeet
people
quotationmark

This is the problem:

AccountManager accountmanager= new AccountManager (account, t, accountmanager);

You're declaring a variable, and trying to read from that variable in the same statement that gives it an initial value.

Here's a simpler example, to show how it's nonsensical:

int y = 10;
int x = y + x;

What would you expect the value of x to be at the end of that? You've said its initial value should be the value of y plus its current value... but it doesn't have a current value, because you're trying to find its initial value!

I strongly suspect that you shouldn't have that their constructor parameter at all though. Why does one AccountManager need a reference to another? You should:

  • Remove the accountmanager field
  • Remove the accountmanager constructor parameter
  • Change the local variable declaration and initialization to:

    AccountManager accountManager= new AccountManager(account, t);
    

    (Note the capital M in accountManager, to follow Java naming conventions. I'd probably rename t to transaction for clarity too.)

people

See more on this question at Stackoverflow