Forum Stats

  • 3,838,279 Users
  • 2,262,350 Discussions
  • 7,900,575 Comments

Discussions

static variable, Lesson 2

3462211
3462211 Member Posts: 27
edited May 19, 2017 1:07PM in Java Puzzle Ball

Hello,

I've finished both lab 2 and quiz 2, but there's one question keep coming up to my mind.  The scenario for lab 2 is that the bank has one type of SavingsAccount, which earns 2% interest.  The code used to demonstrated this is copy typed as below;

public class SavingsAccount {

     //fields

     private static string accountType = "Savings Account";

     private static double interestRate = 0.02;

     //constructor

     public SavingsAccount(string o, double b) {

          accountOwner =o;

          balance = b;

          setAccountNumber();

     }

}

In the real life scenario, the bank normally have several different types of saving accounts.  Does that mean variable accountType can't be a static variable and has to be initialized in the constructor because it becomes an instance variable?

Thanks,

Carol

T

Answers

  • NickR2600-Oracle
    NickR2600-Oracle Member Posts: 530 Employee
    edited May 18, 2017 3:56PM

    Hi Carol,

    Yep.  I like how you're thinking !   What you're proposing seems more accurate.  You could also make interestRate an instance variable in case the different savings accounts require different rates.  And if the accounts have tiered interest rates, you could even write a method which calculates and edits the interest rate depending on the balance.  Ha ha, but then the lab wouldn't give you the opportunity to make anything static =P

    Nick

    3462211
  • 3462211
    3462211 Member Posts: 27
    edited May 19, 2017 1:07PM

    Hi Nick,

    Thanks a lot for your explanation.  That re-assure me that my understanding of static and/or instance variables is correct.  There're more than a thousand people taking this course, however I quite often feel I'm the only person has problems or questions.  So I really appreciate your participation of discussion to facilitate my learning.

    Carol