今天测试代码时遇到

  • 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的更多相关文章

  1. CentOS 6.5 Maven 编译 Apache Tez 0.8.3 踩坑/报错解决记录

    最近准备学习使用Tez,因此从官网下载了最新的Tez 0.8.3源码,按照安装教程编译使用.平时使用的集群环境是离线的,本打算这一次也进行离线编译,无奈一编译就开始报缺少jar包的错,即使手动下载ja ...

  2. 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 ...

  3. vue踩坑- 报错npm ERR! cb() never called!

    在vue项目中引入饿了么elementUI组件的步骤之中,出现以下的错误: D:\my-project-first>npm i element-ui -S Unhandled rejection ...

  4. 【spring cloud】【IDEA】【maven】spring cloud多模块在idea上使用maven插件打包报错:程序包XXX不存在

    >>>>spring cloud 多模块 >>>>在idea上使用maven插件打包,欲打包成jar包后 进行部署 >>>> 报 ...

  5. zabbix启动报错:Connection to database 'xxx' failed解决方法

    Zabbix 分布式系统监视系统 zabbix是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案. zabbix能监视各种网络参数,保证服务器系统的安全运营:并提供灵活的通 ...

  6. 解决Python报错:local variable 'xxx' referenced before assignment(引)

    解决Python报错:local variable 'xxx' referenced before assignment(引) 解决Python报错:local variable 'xxx' refe ...

  7. 【报错】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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. Codeforces Round #549 div2 1143-B Nirvana 题解

    Kurt reaches nirvana when he finds the product of all the digits of some positive integer. Greater v ...

  2. 本地化ASP.NET core模型绑定错误消息

    默认错误消息: MissingBindRequiredValueAccessor A value for the '{0}' property was not provided. MissingKey ...

  3. [JLOI2010]冠军调查 BZOJ2768 最小割

    题目描述 一年一度的欧洲足球冠军联赛已经进入了淘汰赛阶段.随着卫冕冠军巴萨罗那的淘汰,英超劲旅切尔西成为了头号热门. 新浪体育最近在吉林教育学院进行了一次大规模的调查,调查的内容就是关于切尔西能否在今 ...

  4. zabbix监控nginx进程

    确认nginx有没有安装模块 然后在主站点下添加(二级站点) location /NginxStatus { stub_status on; access_log off; allow 127.0.0 ...

  5. Bootstrap FileInput 上传 中文 API 整理

    Bootstrap FileInput 上传  中文 API 整理 上传插件有很多 但是公司用的就是 Bootstrap FileInput 自己就看了看  会用就行 自己都不知道每个值是干嘛用的就问 ...

  6. Long类型比较不能直接用等于

    在判断两个Long型数据是否相等的时候遇到了一个问题. 使用“==”的疑问 if (user.getId() == admin.getId()) { return true; } else { ret ...

  7. 读经典——《CLR via C#》(Jeffrey Richter著) 笔记_基元类型(二)

    [基元类型推荐] 推荐直接使用 FCL 类型. [理由] 编码时不至于困惑string与String的使用.由于C#的stirng(一个关键字)直接映射到System.String(一个 FCL 类型 ...

  8. webAPI过滤器添加参数签名

    项目需求: 接口对安卓和IOS开发接口,需要房子用户窜改数据请求接口.添加sign签名校验参数. 代码如下:加上特性标签就可以控制部分接口验证 public class SignAuthorizeFi ...

  9. UML箭头

    继承(泛化):用实线空心三角箭头表示 实现(接口):用虚线空心三角形箭头标示 依赖:虚线箭头,类A指向类B 方法参数需要传入另一个类的对象,就表示依赖这个类 关联:实线箭头,类A指向类B 一个类的全局 ...

  10. C++: int int& int * int**的区别、联系和用途

    1.int; int是C++关键字,表示整型,其大小是32位有符号整型,表示的范围是-2,147,483,648 到 2,147,483,647:在声明和定义变量时使用,它表示的意思是所声明或所定义的 ...