数组列表对象帮助(静态错误)

时间:2015-04-13 06:17:27

标签: object arraylist static

import java.util.Scanner;
import java.util.ArrayList;
public class fester
{
    public static void main(String args[] )
    {

        ArrayList<BankAccount> ba = new ArrayList<BankAccount>();
        ba.add(new BankAccount("hi", 4));

    }

    class BankAccount
    {
        private String name;
        private double amount;
        public BankAccount(String name, Double amount)
        {
            this.name = name;
            this.amount = amount;
        }

        public String getName()
        {
            return this.name;
        }

        public double getAmount()
        {
            return this.amount;
        }
    }
}

我没有问题。我试图几乎复制这个

http://www.java2s.com/Code/Java/Collections-Data-Structure/Storeuserdefinedobjectsinarraylist.htm

它有效。我很失落,我无法看到根本的差异。

1 个答案:

答案 0 :(得分:0)

您将类BankAccount构造为嵌套的内部类(这意味着您需要外部类的对象才能实例化它)。

将它移到溃烂之外,用4.0代替4,它将工作:

class fester {
    public static void main(String args[]) {    
        ArrayList<BankAccount> ba = new ArrayList<BankAccount>();
        ba.add(new BankAccount("hi", 4.0));    
    }    
}

class BankAccount {
    private String name;
    private double amount;
    public BankAccount(String name, Double amount) {
        this.name = name;
        this.amount = amount;
    }

    public String getName() {
        return this.name;
    }

    public double getAmount() {
        return this.amount;
    }
}

评论:
您应遵循Java命名约定并将fester重命名为Fester(带大写字母)。