【问题标题】:JAVA Eclipse "accessor" cannot be resolved to a variableJAVA Eclipse“访问器”无法解析为变量
【发布时间】:2016-04-03 05:41:55
【问题描述】:

我正在编写一个程序,我需要为变量 accountID 创建访问器/修改器方法。 这是我到目前为止所拥有的,但是当我创建访问器 public int getAccountID() 时,我无法克服这个“无法解析为变量”错误。如何修复此错误?我已经通过其他来源寻找了大约一个小时,但没有一个有帮助,这就是为什么我求助于发布这个特定问题的原因。任何帮助表示赞赏。

import java.util.Scanner;
import java.util.Date;

public class Account {
    public static void main(String[] args) {
        int accountID = 0;
        double balance = 0;
        double annualInterestRate = 0;

        Date dateCreated = new Date();

    }

    // default constructor that creates a default account
    public Account() {
        // fill this in later
    }

    // default constructor that creates an account
    public Account(int accountID, double balance, double annualInterestRate) {
        // fill this in later
    }

    // accessor for accountID
    public int getAccountID() {
        return accountID;     // THIS IS WHERE I GET MY ERROR ~*~*~*~*~*~*~*~*~*~*~*
    }
}

【问题讨论】:

    标签: java eclipse accessor


    【解决方案1】:

    您的accountID(以及main 中定义的其他变量)不应是局部变量。它应该在类级别声明为实例变量,可以从类的所有非静态方法访问。

    public class Account {
        private int accountID = 0;
        private double balance = 0;
        private double annualInterestRate = 0;
        private Date dateCreated = new Date();
    
        public static void main(String[] args)
        {
            ...
        }
    
        ....
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-27
      • 2013-09-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多