【踩坑】报错 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 ...
随机推荐
- Wet Shark and Bishops(思维)
Today, Wet Shark is given n bishops on a 1000 by 1000 grid. Both rows and columns of the grid are nu ...
- jq 使用手册
翻译整理:Young.J官方网站:http://jquery.com jQuery是一款同prototype一样优秀js开发库类,特别是对css和XPath的支持,使我们写js变得更加方便!如果你不是 ...
- BaseNavigationController自定义导航栏
#import <UIKit/UIKit.h> @interface RCDNavigationViewController : UINavigationController<UIG ...
- VS2008 生成的程序有管理员权限
vs 2008 . 解决方案---右键属性----连接器---清单文件---UAC执行级别---设置为requireAdministrator
- react 的安装和案列Todolist
react 的安装和案列Todolist 1.react的安装和环境的配置 首先检查有没有安装node.js和npm node -v npm -v 查看相关版本 2.安装脚手架工具 2.构建:crea ...
- JS执行顺序-函数声明提升、匿名函数、函数表达式
大方向上: JS 是按照 代码块 进行 编译.执行 的. 学习至: 1.变量声明提升 2.新唐的博客 3.js中匿名函数的创建与调用方法分析 4.前端圣经 - <高程三> 5.深入理解变量 ...
- 爬取猫眼电影top100的代码
废话不说,代码附上: #encoding:utf-8 import requests import re import json from multiprocessing import Pool #多 ...
- Experimental Educational Round: VolBIT Formulas Blitz C
Description The numbers of all offices in the new building of the Tax Office of IT City will have lu ...
- sqlserver 数据库表分区
参考文档 https://msdn.microsoft.com/zh-cn/library/ms345146(SQL.90).aspx http://blog.sina.com.cn/s/blog_4 ...
- sql函数将1,2,3转换为表
/****** Object: UserDefinedFunction [dbo].[splitstring_to_table] Script Date: 2017/7/11 9:35:58 **** ...