Discussions
Categories
- 196.9K All Categories
- 2.2K Data
- 239 Big Data Appliance
- 1.9K Data Science
- 450.3K Databases
- 221.7K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 31 Multilingual Engine
- 550 MySQL Community Space
- 478 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3K ORDS, SODA & JSON in the Database
- 545 SQLcl
- 4K SQL Developer Data Modeler
- 187K SQL & PL/SQL
- 21.3K SQL Developer
- 295.9K Development
- 17 Developer Projects
- 138 Programming Languages
- 292.6K Development Tools
- 107 DevOps
- 3.1K QA/Testing
- 646K Java
- 28 Java Learning Subscription
- 37K Database Connectivity
- 155 Java Community Process
- 105 Java 25
- 22.1K Java APIs
- 138.1K Java Development Tools
- 165.3K Java EE (Java Enterprise Edition)
- 18 Java Essentials
- 160 Java 8 Questions
- 86K Java Programming
- 80 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 204 Java User Groups
- 24 JavaScript - Nashorn
- Programs
- 440 LiveLabs
- 38 Workshops
- 10.2K Software
- 6.7K Berkeley DB Family
- 3.5K JHeadstart
- 5.7K Other Languages
- 2.3K Chinese
- 171 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 232 Portuguese
Lab 4

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
Answers
-
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
-
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
-
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