【发布时间】:2014-12-25 01:21:27
【问题描述】:
我尝试使用 netbeans IDE 在 java 中创建一个简单的类。每当我尝试执行此操作时,它都会发出这样的警告。“从静态上下文引用的非静态变量”。谁能告诉我它为什么会发生以及如何解决它。提前谢谢。
public class HW3Q4 {
class Payment{
private double amount_payment;
public void set_amount(double amount){
amount_payment = amount;
}
public double get_amount(){
return amount_payment;
}
public void paymentDetails(){
System.out.println("The amount of the payment is: "+amount_payment);
}
}
public static void main(String[] args) {
// TODO code application logic here
Payment p1 = new Payment();
p1.set_amount(34000.00);
p1.get_amount();
p1.paymentDetails();
}
}
【问题讨论】:
-
Payment p1 = new HW3Q4().new Payment();As Payment 是内部类 -
或者,看起来你想要
static class Payment。 -
class Payment应声明为static。 -
或者更有可能的是,您不需要在这里创建
Payment一个内部类。
标签: java class oop object static