- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这个问题在这里已经有了答案:
Cannot make a static reference to the non-static method
(8 个回答)
7年前关闭。
这是我的第一篇文章,所以格式方面的人要轻松。另外,我对 Java 也很陌生。我的程序编译器出现错误,我不知道如何修复它。我知道我必须为我的静态函数创建一个实例,但我该怎么做呢?
import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.io.*;
public class TestAccount{
public static void main (String[] args) {
Account account = new Account(1122, 20000);
Account.setAnnualInterestRate(4.5);
account.withdraw(2500);
account.deposit(3000);
System.out.println("Balance is " + account.getBalance());
System.out.println("Monthly interest is " +
account.getMonthlyInterest());
System.out.println("This account was created at " +
account.getDateCreated());
}
}
class Account {
private int id=0;
private double balance = 0;
private static double annualInterestRate = 0;
private Date dateCreated;
public Account()
{
this.id =0;
this.balance =0;
this.annualInterestRate =0;
this. dateCreated = new Date();
}
public Account(int id, double balance)
{
this.id =id;
this.balance =balance;
this. dateCreated = new Date();
}
public void setID(int id) {
this.id=id;
}
public int getID() {
return this.id;
}
public void setBalance(double balance) {
this.balance = balance;
}
public double getBalance(){
return this.balance;
}
***public static void setAnnualInterestRate(double annualInterestRate){
this.annualInterestRate = annualInterestRate;
}***
public double getAnnualInterestrate() {
return this.annualInterestRate;
}
public Date getDateCreated() {
return this.dateCreated;
}
public double getMonthlyInterest() {
return (this.annualInterestRate)/12;
}
public void withdraw(double amount) {
this.balance -=amount;
}
public void deposit(double amount)
{
this.balance += amount;
}
}
最佳答案
这里有一个问题:
public static void setAnnualInterestRate(double annualInterestRate){
this.annualInterestRate = annualInterestRate;
}
this
引用一个对象,但该方法是静态的,因此该方法无权访问单个对象。删除
this
关键词。我还建议查看
static keyword. 的含义。
关于java - "non static method cannot be referenced from a static context"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29529567/
我不确定这个语法是否是 Informix 特有的,但是我在创建触发器时遇到了麻烦,直到我找到了一些包含这些行之一或两者的语法: CREATE TRIGGER accuplacer_trig
我是 Android Studio 和 gradle 的新手。我有一个 Android 项目,部分在 Eclipse 中工作。原始代码在Android中使用derby数据库。旧代码通过 JDBC 直接
我对以下示例中的行为感到困惑。我在一个项目中偶然发现了这个问题,我花了几个小时将问题缩小到一个简单的例子。所以这是我的简单测试类: 'John']]; public function inf
我在 pgAdmin 4 中创建了一些表,但由于某种原因我一直收到标题错误,你能找出原因吗?我没有看到任何问题,并且我已经查看了与我的代码示例相似的其他代码示例,这些示例编译得很好。它在 IDEone
我正在创建一个表,但出现此错误: number of referencing and referenced columns for foreign key disagree. 不知道怎么解决。我认为声
假设我有一个名为“C”的库 (.NETStandard 2.0),它定义了一个名为“CRecord”(记录)的类型。 假设我从名为“B”的 .NET 4.7.2 库中使用这个库。有一个使用“CReco
我已经尝试解决这个错误几分钟了,但我不知道表定义中缺少什么。 表格的代码如下: 表Autocare: CREATE TABLE [dbo].[Autocare] ( [IDAutocar]
DROP DATABASE IF EXISTS ProviderPatients; CREATE DATABASE ProviderPatients; USE ProviderPatients; CR
假设我有这个表stuff_property: | stuff_id (fk) | property_id | | ------------- | ----------- | 现在我想要进行查询,该查询
我是 MySQL 的新手。我完全有能力进行查询和创建表,但之前从未尝试过触发器。 CREATE TRIGGER TrigMora AFTER INSERT ON cliente REFEREN
我可以创建一个具有四个这样的属性的对象 $pocketKnife = New-Object PSObject -property @{ Color = 'Black' Weight =
我有一个名为 App 的对象,它包含主干应用程序的所有相关部分。 问题:当我从应用程序中的其他对象调用应用程序中的对象时,它们是未定义的。 我认为发生这种情况是因为在定义其自身的 App 对象之前,它
我有一个ArrayClass并且mergeSortArray扩展了它。并且 mergeSortArray 包含一个 mergeSort() 方法。但是,由于我使用 super 从父类(super cl
public class foo{ private String label; foo(String whereto){ label = whereto; } publi
我正在尝试以编程方式将库添加到引用的库中。这是我的代码: String filename = "myfile.jar"; InputStream is; try { is = new Buffe
我使用@Reference来获取我需要的所有信息: 吗菲亚: Query query = INSTANCE.createQuery(User.class); return query.asLi
我在 eclipse 中有一个 Java 项目,我想在其中添加 3 个 jar 文件到构建路径: 但是,当我选择它们并将它们添加到构建路径(右键单击/构建路径/添加到构建路径)时,它们将与成为类的“j
希望获得 Java 遵循的一些幕后内存引用和规则。 这是一段代码。基本上,此类用于实例化一些其他对象 (MyOtherObject),然后将此对象的 doClose() 方法的引用发送到 Vector
我打开了this关于转发引用的帖子,这是一个(希望如此)MCVE 代码: #include #include using namespace std; struct MultiMemoizator
MySQL 表: categoryID categoryName categoryParent 每个类别都有一个父类别,尽管它可以是 NULL,我将其视为根类别。 我想从表中获取所有类别,将其存储
我是一名优秀的程序员,十分优秀!