非常常见的初学者错误是当您尝试"静态"使用类属性而不创建该类的实例时.它会留下您提到的错误消息:
您可以将非静态方法设为静态,也可以使该类的实例使用其属性.
为什么?我不是要求解决方案.我很高兴知道它背后的原因是什么.核心原因!
private java.util.List<String> someMethod(){
/* Some Code */
return someList;
}
public static void main(String[] strArgs){
// The following statement causes the error. You know why..
java.util.List<String> someList = someMethod();
}
Run Code Online (Sandbox Code Playgroud) 用Java构建多语言应用程序.从R.string资源XML文件插入String值时出错:
public static final String TTT = (String) getText(R.string.TTT);
Run Code Online (Sandbox Code Playgroud)
这是错误消息:
错误:无法从Context类型对非静态方法getText(int)进行静态引用
这是怎么造成的,我该如何解决?
如果此代码格式不正确,我会提前道歉,尝试粘贴而不是重新输入每一行.如果它不对,有人可以告诉我一次粘贴多行代码的简单方法吗?
我的主要问题是我不断收到一条错误消息: Cannot make a static reference to the non-static field balance.
我试过使方法静态,没有结果,并通过从标题中删除"静态"使主方法非静态,但后来我收到消息: java.lang.NoSuchMethodError: main Exception in thread "main"
有没有人有任何想法?任何帮助表示赞赏.
public class Account {
public static void main(String[] args) {
Account account = new Account(1122, 20000, 4.5);
account.withdraw(balance, 2500);
account.deposit(balance, 3000);
System.out.println("Balance is " + account.getBalance());
System.out.println("Monthly interest is " + (account.getAnnualInterestRate()/12));
System.out.println("The account was created " + account.getDateCreated());
}
private int id = 0;
private double balance = 0;
private double annualInterestRate = 0;
public java.util.Date dateCreated;
public Account() …Run Code Online (Sandbox Code Playgroud)