“Cannot make a static reference to the non-static method”处理方法
报错原文:Cannot make a static reference to the non-static method maxArea(Shape[]) from the type ShapeTestb
报错原因:在一个类中写了一个public void maxArea ()方法和一个main()方法,在main()方法中直接调用maxArea()方法就出现如题的错误。
解决方法,有两种:
方法一、maArea()定义时加上修饰符static,即变为public static void maxArea(),即可在main()方法中正确调用。
public class ShapeTestb {
public static void main(String[] args) {
Shape[]shapes = new Shape[2];
maxArea(shapes);
}
//在main()中编译错误
//public void maxArea(Shape[] shapes) {
//在main()中编译正确
public static void maxArea(Shape[]shapes) {
…………
}
}
方法二、先实例化类,然后通过对象调用maxArea()方法也行。
public class ShapeTestb {
public static void main(String[] args) {
Shape[] shapes = new Shape[2];
ShapeTestb st = new ShapeTestb();
st.maxArea(shapes);
}
public void maxArea(Shape[] shapes) {
…………
}
}
“Cannot make a static reference to the non-static method”处理方法的更多相关文章
- Cannot make a static reference to the non-static
public class SeckillServiceImpl implements SeckillService{ private SeckillDao seckillDao; private Su ...
- java 中 Cannot make a static reference to the non-static 解决方法
今天敲代码的时候遇到了这个问题,大体这个问题可以简化成这样: public class Test1 { public String get() { return "123"; } ...
- Cannot make a static reference to the non-static method的解决方法
报错原因:在一个类中写了一个public String getContent()方法和一个main()方法,getContent()方法中包含了getClass()方法,在main()方法中直接调用了 ...
- static 类成员变量 和 static const类成员变量
1.使用static类的优点: (1)避免与其他类的成员或者全局变量冲突 (2)可以封装 (3)阅读性好 2.static 数据成员独立于该类的任意对象而存在 static数据成员的类型可以是该成员所 ...
- static与C#中的static
Static 1.静态方法与非静态方法 a.静态方法的调用类.静态方法名([参数列表]) 非静态方法的调用类 对象 = new 类的构造函数([参数列表])对象.非静态方法名([参数列表]) 静态方法 ...
- public static void main(String[] args){} 关于Java main()方法
是Java程序的入口方法,JVM在运行程序时,会首先查找main()方法. public是权限修饰符,表明任何类或对象都可以访问这个方法: static表明main()方法是一个静态方法,即方法中的代 ...
- java中static特殊性和final(static成员直接被访问,this不能用在static方法中,static不可访问非static)
java的static关键字 java中,static修饰的成员变量和成员方法叫静态变量和静态方法,不依赖类特定的实例,被类的所有实例共享. 静态变量或类变量 和 实例变量,区别是: 静态变量在内存中 ...
- Java关键字final、static使用总结 (final static在容器中不可以改变容器但可以改变存放)
一.final 根据程序上下文环境,Java关键字final有“这是无法改变的”或者“终态的”含义,它可以修饰非抽象类.非抽象类成员方法和变量.你可能出于两种理解而需要阻止改变:设计或效 ...
- java中静态代码块的用法 static用法详解和static静态导入
(一)java 静态代码块 静态方法区别一般情况下,如果有些代码必须在项目启动的时候就执行的时候,需要使用静态代码块,这种代码是主动执行的;需要在项目启动的时候就初始化,在不创建对象的情况下,其他程序 ...
随机推荐
- python模块安装报错大全
报错 环境 解决 手动安装pip install mysqlclient 报错: _mysql.c(29) : fatal error C1083: Cannot open include file: ...
- 把url链接转换成二维码的工具类
import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.io ...
- python-day79--知识回顾
内容回顾: 1. 可迭代对象.迭代器.生成器是什么?什么区别? 可迭代对象,含有__iter__,返回一个迭代器 迭代器,含有__iter__,__next__方法 生成器,yield,__next_ ...
- Vue 项目骨架屏注入与实践
作为与用户联系最为密切的前端开发者,用户体验是最值得关注的问题.关于页面loading状态的展示,主流的主要有loading图和进度条两种.除此之外,越来越多的APP采用了“骨架屏”的方式去展示未加载 ...
- 一、I/O操作(缓存流,数据流,对象流)
一.缓存流 以介质是硬盘为例子说明,字节流和字符流的缺点: 每次读写的时候,都会访问硬盘,如果读写频率比较高的时候,性能不佳.为了解决问题,采用缓存流. 缓存流在读取的时候,会一次性读较多的数据到缓存 ...
- Utils--Cookie工具类
Utils--Cookie工具类 package com.taotao.manage.util; import java.io.UnsupportedEncodingException; import ...
- K-Means ++ 和 kmeans 区别
Kmeans算法的缺陷 聚类中心的个数K 需要事先给定,但在实际中这个 K 值的选定是非常难以估计的,很多时候,事先并不知道给定的数据集应该分成多少个类别才最合适Kmeans需要人为地确定初始聚类中心 ...
- PyQt样式表设置QComboBox
self.comboBox_marital = QComboBox(self) mar_list_view = QListView() self.comboBox_marital.setView(ma ...
- java字符串根据空格截取并存进ArrayList,并在每个元素前后加上/
public class List { public static void main(String[] args) { String s = "abc nnn ooo/xzsxc bs&q ...
- os 模块 和 os模块下的path模块
import os # os 主要用于与操作系统进行交互 #获取当前的工作目录 print(os.getcwd()) #切换工作目录 os .chdir("D:\上海python全栈4期\d ...