Cannot use this in a static context 在main函数中使用了this后,会提示出错。 原因是在main函数中不能使用this,main函数属于static,main中必须生成一个确定的对象来调用方法,凡是属于static的,this均不能用。 我想在main方法中执行 :URL url = this.getClass().getResource("/images/user.jpg"); 报错。 修改...
【情况一】:在静态方法中引用了一个非静态方法 报错:Non-static method 'xxx()' cannot be referenced from a static context 形如: 代码语言:javascript 代码 publicclassMyClass{publicvoidnonStaticMethod(){// 非静态方法实现}publicstaticvoidstaticMethod(){// 在静态方法中引用非静态方法,会导致错误nonStaticMe...
class ToolBox { private static String concatStatic(String str1, String str2) { return str1 + str2; } static String joinTwoStrings(String str1, String str2) { return concatStatic(str1, str2); } } 正如我们在上面的代码中看到的,为了更容易发现我们所做的更改,我们使用了新的方法名称concatSta...
在Java中,静态方法是与类关联的方法,可以直接通过类名调用,而不需要创建类的实例对象。而非静态方法是与实例对象关联的方法,需要通过实例对象来调用。 【情况一】:在静态方法中引用了一个非静态方法 报错: Non-static method 'xxx()' cannot be referenced from a static context 形如: public class MyClass {...
一、问题的解决: on-static method getLastRow() cannot be referenced from a static context问题的出现主要由于是main方法是静态的,如果你在main方法中直接调用一个非静态方法这是不合法的。那么系统就会直接报错。如上述例子中的A.test(1,3);会报错。
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "classpath:spring-mybatis.xml" }) public class SpringMyBatisTest { @Autowired private UserService userService; public static void main(String[] args) ...
this.bottom = bottom;} public double getVolum() { return (bottom.getArea() * height) / 3;} } public class aa { public static void main(String args[]) { Zhui zui;GraphicObject tuxing;tuxing = new TiXing(2, 3, 4);System.out.println("梯形的面积是" + tuxing.getArea()...
non-staticmethod cannot be referenced from a static context。这个错就有点奇怪了,非静态方法不能被一个静态context引用。但问题是我这里都不是静态方法啊,而且是加了nullsLast这个参数后出现的问题,所以肯定是这里的写法有问题,而nullsLast这个函数是有的,那就是里面的参数不对咯,参数有什么问题?这里的String::...
可以的。类可以有内部类,具体问题。具体再追问。错误提示是说:非静态变量类型不能在静态上下文中引用。(1)你的;main方法是static的,即静态的。(2)如果你将类包含在里面的话,需要将printchar声明为静态的。才能在main方法中引用。首先
}publicstaticvoidmain(String[] args){staticStringstring=get(); } } 1 2 3 4 5 6 7 8 9 10 可是还是错的。。。 翻了一下java书才知道 1.java中 静态方法不可以直接调用非静态方法和成员,也不能使用this关键字(这就是这个问题的原因,我用静态的main方法调用了非静态的的get方法)。 原因...