0

Line 07 - I'm getting the following error:

No enclosing instance of type Exercise10_7 is accessible

How can I resolve this? Also, after finishing this I need to modify it to hold an array of 10 accounts with a 4 option menu (check balance, withdraw, deposit, exit). If I could get some guidance on accomplishing this that would be awesome.

import java.util.Date;

public class Exercise10_7 {

    public static void main(String[] args) {

    Account account1 = new Account(1122, 20000, .045);
    account1.withdraw(2500);
    account1.deposit(3000);
    java.util.Date dateCreated = new java.util.Date();

    System.out.println("Date Created:" + dateCreated);
    System.out.println("Account ID:" + account1.id);
    System.out.println("Balance:" + account1.getBalance());
    System.out.println("Interest Rate:" + account1.getAnnualInterestRate());
    System.out.println("Balance after withdraw of 2500:" +       account1.getAnnualInterestRate());
    System.out.println("Balance after deposit of 3000:" + account1.getAnnualInterestRate());
    System.out.println("Monthly Interest:" + account1.id);
    System.out.println("Process completed.");
    }

    class Account {
        //define variables
        private int id;
        private double balance; // balance for account
        private double annualInterestRate; //stores the current interest rate
        private Date dateCreated; //stores the date account created

        //no arg construtor
        Account () {
            id = 0;
            balance = 0.0;
            annualInterestRate = 0.0;
        }
        //constructor with specific id and initial balance
        Account(int newId, double newBalance) {
            id = newId;
            balance = newBalance;
        }
        Account(int newId, double newBalance, double newAnnualInterestRate) {
            id = newId;
            balance = newBalance;
            annualInterestRate = newAnnualInterestRate;
        }
        //accessor/mutator methods for id, balance, and annualInterestRate
        public int getId() {
            return id;
        }
        public double getBalance() {
            return balance;
        }
        public double getAnnualInterestRate() {
            return annualInterestRate;
        }
        public void setId(int newId) {
            id = newId;
        }
        public void setBalance(double newBalance) {
            balance = newBalance;
        }
        public void setAnnualInterestRate(double newAnnualInterestRate) {
            annualInterestRate = newAnnualInterestRate;
        }
        //accessor method for dateCreated
        public void setDateCreated(Date newDateCreated) {
            dateCreated = newDateCreated;
        }
        //define method getMonthlyInterestRate
        double getMonthlyInterestRate() {
            return annualInterestRate/12;
        }
        //define method withdraw
        double withdraw(double amount) {
            return balance -= amount;
        }   
        //define method deposit
        double deposit(double amount) {
            return balance += amount;   
        }
    }
}
Blue
  • 22,608
  • 7
  • 62
  • 92
  • Possible duplicate of [No enclosing instance of type Server is accessible](http://stackoverflow.com/questions/7901941/no-enclosing-instance-of-type-server-is-accessible) – Raedwald Mar 02 '16 at 22:30
  • Possible duplicate of [Java - No enclosing instance of type Foo is accessible](http://stackoverflow.com/questions/9560600/java-no-enclosing-instance-of-type-foo-is-accessible) – fabian Mar 03 '16 at 22:44

2 Answers2

0

Because Account is an inner class, you need an instance of the enclosing class to incarnate it. Or you could make Account a static class. You could either change

class Account {

to

static class Account {

or change

Account account1 = new Account(1122, 20000, .045);

to

Account account1 = new Exercise10_7().new Account(1122, 20000, .045);

JLS-8.1.3. Inner Classes and Enclosing Instances says (in part)

An inner class is a nested class that is not explicitly or implicitly declared static.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

You cannot access non static fields/properties inside static method, main method is static and your inner class is non static. you have two options

  1. make your inner class static
  2. access your inner class from object of your class. e.g YourInnerClass obj = new OuterClass(). new YourInnerClass (parameters)
Sindhoo Oad
  • 1,194
  • 2
  • 13
  • 29