Forum Stats

  • 3,826,752 Users
  • 2,260,703 Discussions
  • 7,897,071 Comments

Discussions

Lab 4

3462211
3462211 Member Posts: 27
edited May 25, 2017 2:40PM in Java Puzzle Ball

Hi Nick,

I apologize I have so many questions as regards to lesson 4.  So far I have 2 questions about lab 4.

1) Why is that instance field variable transactions is not initialized to 0 in the constructor of abstract class Account?

For every newly created account object, I assume the number of transaction is expected to be zero.  Its value is vary on different account objects, so it makes sense it's not being declared/defined as a static variable.  There's also a resetTransactions method can be used/called to automatically reset the number of transactions to zero at the beginning of each month. That means the value of variable transactions can be maintained well.

2) Variable accountList for ArrayList instance is already declared in the Buttoncontroller class.  Why is that it is being declared again in the NewFXMain?

That particular line of code is copy-typed as following;

ArrayList<Account>  accountList = new ArrayList<>();

Thanks,

Carol

3462211

Answers

  • NickR2600-Oracle
    NickR2600-Oracle Member Posts: 530 Employee
    edited May 24, 2017 4:24PM

    Hi Carol,

        I don't mind the questions, not at all.  One of the benefits of the game is that it spurs people have conversations about Java.  I'm glad you're poking around the code and being curious.  And if any lesson were to give people trouble, it would be the final lesson.

    1) You could initialize the transactions field to 0 from the Account constructor.  However, it's not necessary.  If a number field (like an int or double) isn't explicitly given an initial value, its value becomes 0.  In other words, it defaults to 0.  Correct, transactions would be an instance variable.

    2) NewFXMain is where the ArrayList is first created and where account instances are added to that list.  But while that's going on, the ButtonController class has no idea that particular ArrayList even exists!  All it knows is that it'll have to work with some sort of ArrayList.  A buttonController instance wouldn't be able to work with an ArrayList unless we explicitly tell it which ArrayList instance we're talking about.  This line of code from NewFXMain creates a ButtonController instance named buttonController and tells it which ArrayList to use:

    ButtonController buttonController = new ButtonController(accountList, ownerSearchBar, numberSearchBar, btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8);

    The ButtonController class saves this ArrayList as a field.  The variable used to save this information is coincidentally is also called accountList.  We could name the variable whatever we want, and it will still point to the same ArrayList created in NewFXMain.

    Nick

    34622113462211
  • 3462211
    3462211 Member Posts: 27
    edited May 25, 2017 1:52AM

    Thanks a lot for your explanation Nick.  For question 1), I should have copy-typed your code (please see below) or worded my question a bit better.  I think I meant to ask:  Is there any particular reason you choose to initialize the instant variable transactions in the field declaration area instead of constructor?

    public abstract class Account {

        //Fields

        protected String accountOwner;

        protected double balance;

        protected int accountNum;

        protected int transactions = 0;

        protected static int nextAccountNum = 0;

       

       

        //Constructor

        public Account(String o, double b){

            accountOwner = o;

            balance = b;

            setAccountNumber();

            System.out.println("New Account:");

            printDetails();

        }

    Carol

  • NickR2600-Oracle
    NickR2600-Oracle Member Posts: 530 Employee
    edited May 25, 2017 2:40PM

    Hmm... I may have been thinking "For every field which doesn't require a value to be passed to the constructor, I'll set its initial value where the field is declared." 

    Nick