Reputation: 21
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.
Upvotes: 0
Views: 153
Reputation: 10341
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;
}
}
Upvotes: 0
Reputation: 347204
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
Upvotes: 1