eclipse Cannot make a static reference to the non-static method

今天写leetcode的时候用eclipse调试,然后出现了Cannot make a static reference to the non-static method这个问题,就是main函数是static的,不能在里面使用非静态方法或成员(this调用类成员),因为静态的函数在编译时期就已经存在于.class文件中,而动态的方法或成员是在运行时才对象实例化,所以在编译时期无实例化对象时无法调用动态的方法或对象。
解决方法:一个是把你原来的方法改成静态的,但是改动比较多。另一个就是在静态函数内声明一个对象,然后再调用对象的方法。

public class leetcode{
	public void helper(){}
	Object object;
	public static void main(String args[])
	{
		leetcode a=new leetcode();
		a.helper();
		a.object;
	}
}

ps:eclipse一定要有main方法才能调试。

发布了45 篇原创文章 · 获赞 4 · 访问量 1044

猜你喜欢

转载自blog.csdn.net/weixin_43838915/article/details/104845734