【踩坑】报错 non-static method xxx() cannot be referenced from a static context
今天测试代码时遇到
- Error:(6, 55) java: non-static method sayGoodbye() cannot be referenced from a static context
的报错,代码如下:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Greeting: " + GoodByeWorld.sayGoodbye());
}
}
class GoodByeWorld {
public String sayGoodbye() {
return "GoodBye";
}
}
原因:
不能直接调用类变量和类方法。
解决方法一:
将方法改成类方法,如下:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Greeting: " + GoodByeWorld.sayGoodbye());
}
}
class GoodByeWorld {
public String static sayGoodbye() {
return "GoodBye";
}
}
解决方法二:
生成实例,调用实例中的非静态方法,如下:
public class HelloWorld {
public static void main(String[] args) {
GoodByeWorld greeting = new GoodByeWorld();
System.out.println("Greeting: " + greeting.sayGoodbye());
}
}
class GoodByeWorld {
public String sayGoodbye() {
return "GoodBye";
}
}
【踩坑】报错 non-static method xxx() cannot be referenced from a static context的更多相关文章
- CentOS 6.5 Maven 编译 Apache Tez 0.8.3 踩坑/报错解决记录
最近准备学习使用Tez,因此从官网下载了最新的Tez 0.8.3源码,按照安装教程编译使用.平时使用的集群环境是离线的,本打算这一次也进行离线编译,无奈一编译就开始报缺少jar包的错,即使手动下载ja ...
- Vue报错:Property or method "XXX" is not defined on the instance but referenced during render. Make sure that this property is reactive...
在Vue中定义方法或者属性时,因为粗心疏忽可以能会报该错误 [Vue warn]: Property or method "search" is not defined on th ...
- vue踩坑- 报错npm ERR! cb() never called!
在vue项目中引入饿了么elementUI组件的步骤之中,出现以下的错误: D:\my-project-first>npm i element-ui -S Unhandled rejection ...
- 【spring cloud】【IDEA】【maven】spring cloud多模块在idea上使用maven插件打包报错:程序包XXX不存在
>>>>spring cloud 多模块 >>>>在idea上使用maven插件打包,欲打包成jar包后 进行部署 >>>> 报 ...
- zabbix启动报错:Connection to database 'xxx' failed解决方法
Zabbix 分布式系统监视系统 zabbix是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案. zabbix能监视各种网络参数,保证服务器系统的安全运营:并提供灵活的通 ...
- 解决Python报错:local variable 'xxx' referenced before assignment(引)
解决Python报错:local variable 'xxx' referenced before assignment(引) 解决Python报错:local variable 'xxx' refe ...
- 【报错】spring整合activeMQ,pom.xml文件缺架包,启动报错:Caused by: java.lang.ClassNotFoundException: org.apache.xbean.spring.context.v2.XBeanNamespaceHandler
spring版本:4.3.13 ActiveMq版本:5.15 ======================================================== spring整合act ...
- springAOP注解方式定义切入点报错error at ::0 can't find referenced pointcut
[说明] 1.使用spring版本:4.0.4 2.springAOP相关依赖包: 1)aopalliance-1.0.jar 2)aspectjweaver-1.8.9.jar 3)aspectjr ...
- eclipse 中离线安装activiti插件,报错“An error occurred while collecting items to be installed session context was:(...”
eclipse 中离线安装activiti插件,报错“An error occurred while collecting items to be installed session context ...
随机推荐
- js判断页面从何种浏览器打开
问题 有时项目需要根据不同的设备进行不同的处理,需要判断到底是哪种设备打开了项目. 移动端浏览器检测 移动终端浏览器版本信息: var browser = { versions: function ( ...
- [SinGuaRiTy] 2017-07-24 NOIP2015 模拟赛
[SinGuLaRiTy-1030] Copyright (c) SinGuLaRiTy 2017. All Rights Reserved. 对于所有题目: Time Limit: 1s | Mem ...
- JAVA环境的JAVA_HOME, PATH 和CLASS_PATH设置
Windows下JAVA用到的环境变量主要有3个,JAVA_HOME.CLASSPATH.PATH.下面逐个分析. 简单来讲, 1.path是os用 classpath java用 JAVA_HOME ...
- loj#6040. 「雅礼集训 2017 Day5」矩阵(线性代数+递推)
题面 传送门 题解 我的线代学得跟屎一样看题解跟看天书一样所以不要指望这题我会写题解 这里 //minamoto #include<bits/stdc++.h> #define R reg ...
- cuda&vs2010的属性配置
平时总需要新建工程,但是却总忘记该修改哪里,于是寻找了官方的项目,截下其中的属性修改图. 1 2 3 4 5 6 7 8 9 10 11
- #6145. 「2017 山东三轮集训 Day7」Easy 动态点分治
\(\color{#0066ff}{题目描述}\) JOHNKRAM 最近在参加 C_SUNSHINE 举办的聚会. C 国一共有 n 座城市,这些城市由 n−1 条无向道路连接.任意两座城市之间有且 ...
- Jenkins 相关
手动下载Jenkins plugin 的地址, 下载后的是zip 文件,然后再手动修改为.hpi 文件,然后再手动上传 https://plugins.jenkins.io/
- [Android UI]View滑动方式总结
一.前言 在上一篇文章,介绍了View的坐标等基础知识,有了基础知识后,对下面内容的理解也将会容易很多.那么本文介绍的是View滑动的几种方式,这对于View来说,也是需要重要掌握的内容,因为用户无时 ...
- POJ2886Who Gets the Most Candies?(线段树之约瑟夫)
约瑟夫问题的升级版,每次出去的是前一个出去的人位置+手上的数字(正往前,负往后).第i个出去的人拿的糖是i的约数的个数.求拿糖最多的人和他的糖果数. 这里用到了反素数的知识,在这直接打表 题目 AC代 ...
- Educational Codeforces Round 3 B
B. The Best Gift time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...