Java问题记录|Non-static method cannot be referenced from a static context
2020-03-28 20:14
3615 查看
问题:
原因:
静态方法无法调用自己定义的非静态方法
解决:
1.改变非静态方法为静态方法,在add方法中的void前加static
2.可在main主方法里实例化非静态方法的对象
以下为例:
public class Demo01 { public static void main(String[] args) { //静态方法的调用 Demo01.add(); //非静态方法的调用 Demo01 demo=new Demo01();//实例化对象,再调用方法 demo.get(); } //静态方法 public static void add(){ System.out.print("静态方法"); } //非静态方法 public void get(){ System.out.print("非静态方法"); } }
- 点赞
- 收藏
- 分享
- 文章举报
相关文章推荐
- Non-static method cannot be referenced from a static context
- Java - Why non-static variable cannot be referenced from a static context?
- Non-static method 'xxx cannot be referenced from a static context原因之一
- 问题1-The type java.lang.String cannot be resolved. It is indirectly referenced from required .class files
- 解决问题: ----- The type java.io.ObjectInputStream cannot be resolved. It is indirectly referenced from
- Non-stati 4000 c method 'findViewById(int)' cannot be referenced from a static context
- Non-static field 'mSwipeRefreshLayout' cannot be referenced from a static context
- non-static variable this cannot be referenced from a static context
- 记录no static method cannot be reference
- The type java.lang.Object cannot be resolved. It is indirectly referenced from required .class files
- The type java.util.Map$Entry cannot be resolved. It is indirectly referenced from required .class fi
- The type java.lang.Object cannot be resolved.It is indirectly referenced from required .class file
- The type java.lang.Object cannot be resolved.It is indirectly referenced from required.class files
- android 引入别的项目The type java.lang.Class cannot be resolved. It is indirectly referenced from required
- The type java.util.Map$Entry cannot be resolved. It is indirectly referenced from required .class fi
- The type javax.servlet.ServletContext cannot be resolved. It is indirectly referenced from required
- The type java.lang.Object cannot be resolved. It is indirectly referenced from required .class files
- The type java.lang.Object cannot be resolved. It is indirectly referenced from required .class files
- The type java.lang.CharSequence cannot be resolved. It is indirectly referenced from required .class files.
- the type java.io.ObjectInputStream cannot be resolved. it is indirectly referenced from required .cl