Discussions
Categories
- 196.9K All Categories
- 2.2K Data
- 239 Big Data Appliance
- 1.9K Data Science
- 450.4K 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
- 546 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
- 442 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
I am really confused on how to identify the solution to Lab 2. Can you help?

I am really confused on how to identify the solution to Lab 2. Can you help?
Answers
-
Of course. That's exactly the kind of thing the forum is for. Where is it you're having trouble?
-
Someone else sent an email about Lab 2. I'll post a general summary of what I sent so everyone can benefit.
t, o, b, and r are created within the Constructor and don't exist outside the scope of that constructor. The official name for them are "method parameters". Private class fields like accountType can't be accessed outside the scope of the SavingsAccount class. But what we can do is supply values to t, o, b, and r by calling the constructor. This is done in the main method where savings account instances are created. To do any sort of calculations with these values later, we need to somehow get these values out of the constructor and store them somewhere a little more permanent. That's why the constructor takes these values and uses them to set the initial values of each class field. A class can access and change its field values whenever it wants.
You may also see people who name their constructor variables the same as their fields. When this happens, they use the keyword "this" to differentiate the field from the temporary constructor variable:
public class SavingsAccount { //Fields private String accountType; private String accountOwner; private double balance; private double interestRate; private int accountNum; private static int nextAccountNum = 1; //Constructor public SavingsAccount(String accountType, String accountOwner, double balance, double interestRate){ this.accountType = accountype; this.accountOwner = accountOwner; this.balance = balance; this.interestRate = interestRate; setAccountNumber();
-
And here's part 2:
If you look at the SavingsAccount fields from Lab 1, you'll see that we're setting the initial field values as soon as the field variables are declared:
private String accountType = "Savings Account"; private String accountOwner = "Duke"; private double balance = 0.0; private double interestRate = 0.02;
The problem with this is that it doesn't provide much customization. For example, all of the accountOwners are going to be called Duke! It's possible to write methods to change these values one-by-one, but that would lead to really verbose looking code, which is harder to maintain and a pain to write for. A constructor is a convenient and elegant way to set many initial field values within a single method call.
If you choose not to set a variable's value when it's declared or through a method, the variable keeps what's considered it's"default value". Default values are sometimes useful. They depend on the variable's data type. A String will have a default value of 'null'. A double or integer will have a default value of zero. But sometimes they're not useful. If you try to use null or 0 for calculations or whatever, you run the risk of getting "null pointer" errors or "cannot divide by 0" errors.