Java 非静态变量不能从静态上下文行16引用此变量
此代码的第16行中有一个错误。关于静态的东西,我想不出来,任何想法都会有帮助Java 非静态变量不能从静态上下文行16引用此变量,java,static,Java,Static,此代码的第16行中有一个错误。关于静态的东西,我想不出来,任何想法都会有帮助 package sample1; import java.util.ArrayList; import java.util.Date; public class Sample1 { //main class /** * @param args the command line arguments */ public static void main(String[]
package sample1;
import java.util.ArrayList;
import java.util.Date;
public class Sample1 {
//main class
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Account acc1 = new Account("George", 1122, 1000, .015);
System.out.print("The account holder's name: " + acc1.getname()
+ ". The annual interest rate: " + acc1.getrate(.015)
+ ". The balance: " + acc1.getbalance() + ". The monthly interest: "
+ acc1.monthlyInterest + ". All the transactions: "
+ acc1.getTransactions());
}
public class Transaction {
private Date datetrans;
private char type;
private double amount;
private double balance;
private String description;
char Withdraw = 'W';
char deposit = 'D';
public Transaction(Date newDateTrans, char newType, double newAmount,
double newBalance, String newDescription) {
datetrans = newDateTrans;
type = newType;
amount = newAmount;
balance = newBalance;
description = newDescription;
datetrans = new Date();
}
Date getdatetrans() {
return datetrans;
}
char gettype() {
return type;
}
double getamount() {
return amount;
}
String getdescription() {
return description;
}
double getbalance() {
return balance;
}
}
public class Account {
private int id = 0;
private String name;
private double balance = 0;
private double rate = 0;
private Date dateAdded;
private ArrayList<String> transactions = new ArrayList();
public Account(String newName, int newId, double newBalance) // this is where the error is, any ideas why it can be {
id = newId;
name = newName;
balance = newBalance;
dateAdded = new Date();
}
int getid() {
return id;
}
double getbalance() {
return balance;
}
double getrate(double newRate) {
return rate;
}
public void setid(int newId) {
id = newId;
}
public void setbalance(double newBalance) {
balance = newBalance;
}
public void setrate(double newRate) {
rate = newRate;
}
String getname() {
return name;
}
String getTransactions() {
return transactions.toString();
}
Date getDateAdded() {
return dateAdded;
}
double monthlyInterestRate;
double monthlyInterest;
String withdraw;
String deposit;
public double getMonthlyInterestRate() {
monthlyInterestRate = rate / 12;
return monthlyInterestRate;
}
public double getMonthlyInterest() {
monthlyInterest = balance * monthlyInterestRate;
return monthlyInterest;
}
String getDeposit() {
return deposit;
}
void addtransactions(String deposit) {
transactions.add(deposit);
}
String getWithdraw() {
return withdraw;
}
void droptransactions(String withdraw) {
transactions.remove(withdraw);
}
}
}
包装样本1;
导入java.util.ArrayList;
导入java.util.Date;
公共类样本1{
//主类
/**
*@param指定命令行参数
*/
公共静态void main(字符串[]args){
//此处的TODO代码应用程序逻辑
账户acc1=新账户(“乔治”,11221000.015);
System.out.print(“账户持有人的姓名:”+acc1.getname()
+年利率:“+acc1.getrate(.015)
+。余额:“+acc1.getbalance()+”。月利息:
+acc1.monthlyInterest+”。所有交易:
+acc1.getTransactions());
}
公共类事务{
私人日期转换;
私有字符类型;
私人双倍金额;
私人双平衡;
私有字符串描述;
char-draw='W';
煤焦沉积物='D';
公共事务(日期newDateTrans、字符newType、双倍newAmount、,
双新天平,字符串新描述){
datetrans=newDateTrans;
类型=新类型;
金额=新金额;
平衡=新平衡;
description=newDescription;
datetrans=新日期();
}
日期getdatetrans(){
返回日期转换;
}
char gettype(){
返回类型;
}
double getamount(){
退货金额;
}
字符串getdescription(){
返回说明;
}
双重getbalance(){
收益余额;
}
}
公共类帐户{
私有int id=0;
私有字符串名称;
私人双平衡=0;
私人双费率=0;
已添加私人日期;
私有ArrayList事务=新建ArrayList();
public Account(String newName,int newId,double newBalance)//这就是错误所在,你知道为什么吗{
id=newId;
name=newName;
平衡=新平衡;
dateAdded=新日期();
}
int getid(){
返回id;
}
双重getbalance(){
收益余额;
}
双getrate(双newRate){
回报率;
}
公共void setid(int newId){
id=newId;
}
公共无效退平衡(双新平衡){
平衡=新平衡;
}
公共无效设置率(双倍新利率){
利率=新利率;
}
字符串getname(){
返回名称;
}
字符串getTransactions(){
return transactions.toString();
}
日期getDateAdded(){
已添加返回日期;
}
双月利率;
双月利息;
抽绳;
串状矿床;
公共双GetMonthlyInterestate(){
月利率=费率/12;
每月归还利息;
}
公共双getMonthlyInterest(){
月利息=余额*月利息;
每月归还利息;
}
字符串getDeposit(){
退还保证金;
}
作废交易记录(字符串存款){
交易。添加(存款);
}
字符串getdraw(){
退换货;
}
作废交易记录(字符串撤消){
交易。移除(撤回);
}
}
}
将事务和帐户移动到它们自己的文件中,与Sample1分开,但在同一个包中(顶部有相同的包声明,与Sample1位于同一目录中,文件名与类名匹配)
嵌套类声明创建内部类,内部类(除非标记为静态)依赖于外部类的引用。他们是一个复杂的你不需要在这里。(或者,您可以将静态添加到内部类的声明中,但最好保持简单。)您有一个嵌套的非静态类,名为
Account
。这意味着要创建这个类的实例,您需要有一个外部类的实例,在本例中是Sample1
。你可以用三种方法修复它
1) 使用外部类的实例
Account acc1 = new Sample1().new Account("George", 1122, 1000, .015);
2) 或者,您可以使类成为静态的,因为它似乎不需要外部类
static class Account {
对于事务,您也可以这样做
3) 不要使用嵌套类并将其上移一级。您的IDE将能够为您执行此重构
除非您有偏好,否则我将使用选项2,因为它的更改最少,很可能这就是您的意图。我们不能在static内部使用非静态变量,因为在加载类时,静态变量内存是首先分配的。因此,在您的代码中,您说的是static main()方法,其内存在运行时分配以创建非静态的实例<代码>此处示例1主函数不知道帐户类是否存在。 为了解决这个问题,你必须在你的类声明上加上一个静态关键字
static class Account{}
阅读内部类。除非有真正的需要,否则不要嵌套类,这是您在此处出错的原因。如果定义了
类事务,但从未使用过,我将如何进行嵌套。第16行为空。你能说是哪一行导致了这个错误吗?我把它们移到了他们自己的文件中,但是我仍然在这个lineAccount acc1=新帐户中得到了一个错误