0

Working through an assignment right now and could use help understanding a concept that is eluding me.

In particular, I have to create an ArrayList to hold two Account objects, and then use that list to collect input. Unfortunately when I try to store those objects into an ArrayList, one of them is giving me problems.

import java.util.ArrayList;

public class InheritanceTest {

    private class Account {
        //Constructor
        Account() {
            // Initialize balance
            int balance = 0;
        }
    }

    private class CheckingAccount extends Account {
    }

    private class SavingsAccount extends Account {
    }

    public static void main(String[] args) {
        ArrayList<Account> Bank = new ArrayList<Account>();
        CheckingAccount checking1 = new CheckingAccount();
        Bank.add(checking1);
        SavingsAccount savings = new SavingsAccount();
        Bank.add(savings);
    }
}

The command to instantiate a new CheckingAccount object works fine, but once I plug the account into Bank.add() the new CheckingAccount object throws the "No enclosing instance of type InheritanceTest is accessible. Must qualify the allocation with an enclosing instance of type InheritanceTest (e.g. x.new A() where x is an instance of InheritanceTest)." error. I imagine there is some basic concept that I've not been able to find. I had all of my classes declared as static, and that allowed me to compile, but the things that I have read have made me think that's not the right way to go about it.

Kyle Meyer
  • 21
  • 1
  • 6
  • What is `InheritanceTest`? – Tom Nov 24 '15 at 23:23
  • 2
    Try changing `private class` to `public class` – MadProgrammer Nov 24 '15 at 23:23
  • You forgot to instantiate the class running main() see http://stackoverflow.com/a/11517185/1665377 – maximede Nov 24 '15 at 23:23
  • Tom, InheritanceTest is just the name of the .java file and so everything is within the InheritanceTest class. – Kyle Meyer Nov 24 '15 at 23:31
  • MadProgrammer, which class in particular should be declared public? It sounds like this isn't the solution I am looking for, as both the SavingsAccount and CheckingAccount classes are private, but the SavingsAccount object isn't have these problems. – Kyle Meyer Nov 24 '15 at 23:33
  • Consider providing a [runnable example](https://stackoverflow.com/help/mcve) which demonstrates your problem. This is not a code dump, but an example of what you are doing which highlights the problem you are having. This will result in less confusion and better responses – MadProgrammer Nov 24 '15 at 23:33
  • @KyleMeyer All of them – MadProgrammer Nov 24 '15 at 23:34
  • Thank you for the advice MadProgrammer, I'll get right on making a runnable example. – Kyle Meyer Nov 24 '15 at 23:35
  • i'm presuming that the above classes are all declared inside of the InheritanceTest class, instead of declared outside. the main method doesn't create an instance of InheritanceTest, so the message is coming out. – John Gardner Nov 24 '15 at 23:43
  • I have edited my post to include a runnable example as MadProgrammer suggested. I chopped out all of the methods and stuff to make it easier to read, and you can still see the same problem. Plug checking1 into Bank.add() and you get an error on the new CheckingAccount command. The weird thing is that the SavingsAccount object doesn't have this issue and as far as I can tell, it's class doesn't differ at all from the CheckingAccount class. – Kyle Meyer Nov 24 '15 at 23:50
  • @MadProgrammer please be sure to let me know if there is anything else I can do to improve the clarity of my question. I appreciate the feedback greatly. – Kyle Meyer Nov 24 '15 at 23:58
  • Interessting that you say *"throws the aforementioned error"*, but you removed the error text ... Please re-insert the correct error message. – Tom Nov 25 '15 at 00:06
  • 1
    So, based on your updated code, you should be getting *"non-static variable cannot be referenced from a static context"*, which k0ner has addressed – MadProgrammer Nov 25 '15 at 00:07
  • @MadProgrammer I'm still getting the same error: No enclosing instance of type InheritanceTest is accessible. Must qualify the allocation with an enclosing instance of type InheritanceTest (e.g. x.new A() where x is an instance of InheritanceTest). – Kyle Meyer Nov 25 '15 at 00:21
  • 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 04 '16 at 00:27

2 Answers2

1

There are three possible solutions to your problem.

First, you can make the inner class's static...

public class InheritanceTest {

    private static class Account {

        //Constructor
        Account() {
            // Initialize balance
            int balance = 0;
        }
    }

    private static class CheckingAccount extends Account {
    }

    private static class SavingsAccount extends Account {
    }

    public static void main(String[] args) {
        ArrayList<Account> Bank = new ArrayList<Account>();
        CheckingAccount checking1 = new CheckingAccount();
        Bank.add(checking1);
        SavingsAccount savings = new SavingsAccount();
        Bank.add(savings);
    }
}

or you need to create an instance of InheritanceTest and use it to create the inner classes

import java.util.ArrayList;

public class InheritanceTest {

    private class Account {

        //Constructor
        Account() {
            // Initialize balance
            int balance = 0;
        }
    }

    private class CheckingAccount extends Account {
    }

    private class SavingsAccount extends Account {
    }

    public static void main(String[] args) {
        InheritanceTest test = new InheritanceTest();
        ArrayList<Account> Bank = new ArrayList<Account>();
        CheckingAccount checking1 = test.new CheckingAccount();
        Bank.add(checking1);
        SavingsAccount savings = test.new SavingsAccount();
        Bank.add(savings);
    }
}

or separate each class into it's own class file

This basically occurs as a non-static inner class can not be created without an instance of the outer class

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0

MadProgrammer already fixed the real issue, just wanted to add one thing. You may want to research 'Variable Scope'.

Your balance variable will be created and immediately destroyed because its scope is limited to the constructor. To fix it, move balance outside of the constructor. Now it will live longer and you can access and change it.

private class Account {
    //declare balance outside of the constructor 
    int balance;
    Account() {
        //initialize balance to zero inside the constructor
        balance = 0;
    }
}
jb.
  • 9,921
  • 12
  • 54
  • 90