繁体   English   中英

“无法从静态上下文引用非静态方法”错误

[英]“Non-static method cannot be referenced from static context” error

我想要做的是在这里添加另一个类( Noten )中的对象并打印出来。 我知道这是一个普遍的问题,但仍然找不到解决方案。

private ArrayList<Noten> notes123;
public void addNotes(Noten newNotes) {
    if (notes123.size() >= 0) {
        notes123.add(newNotes);
        System.out.println(newNotes);
    } else {
        System.out.println("No Notes.");
    }
}
public void schuelerInfo() {
    System.out.println("Name: " + name + " Student number: " + nummer);
    System.out.println("The notes are ");
    for (Noten note: notes123) {
        System.out.println(Noten.notenInfo());
    }
}

从更改您的for循环

for (Noten note : notes123){
   System.out.println(Noten.notenInfo());
}

for (Noten note : notes123){
   note.notenInfo();
}

由于noteInfo方法被定义为非静态方法,因此您尝试使用Noten(类)静态访问它。 您只能在已将引用存储在arraylist中的对象上访问它。

由于notenInfo()不是静态方法,因此必须在Noten对象的实例上调用它。 例如:

Noten n = new Noten();
n.notenInfo();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM