
Java 类的内部嵌套子类型定义 如何正确饮用
在main函数中 申明abc的对象
发布日期:2021-05-07 23:35:37
浏览次数:21
分类:原创文章
本文共 900 字,大约阅读时间需要 3 分钟。
写了一个非常简单的例子
public class test { public class abc { } public static void main (String[] args) { ... }}
在main函数中 申明abc的对象
abc var = new abc();
编译的时候总是报告这个错误
test.java:7: non-static variable this cannot be referenced from a static context
abc var = new abc();
^
1 error
shell returned 1
Press ENTER or type command to continue
调查类一下StackOverflow 上面有如下解释
http://stackoverflow.com/questions/15331846/non-static-variable-this-cannot-be-referenced-from-a-static-context
In Java there are : "Static nested class" and . Without the
static
keyword it is an inner class and you will need an instance of ShowBike
to access ShowBike.Bycicle
:大概意思就是java的嵌套类型有两种一个事静态的一个是非静态的。因为main函数是静态的所以需要调用静态的内嵌子类。如果一定要吊用非静态的子类需要先申明一个父类的对象然后才能调用子类。
于是做了以下实验
public class test { public static class abc { } public static void main(String[] args) { abc var = new abc(); } }
这样代码就可以编译通过.
发表评论
最新留言
关注你微信了!
[***.104.42.241]2025年03月25日 16时50分52秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Node.js包使用系列(一)——修改NPM全局下载和缓存路径
2019-03-05
TDengine使用(一)——TDengine下载与安装
2019-03-05
ubuntu和windows之间无法复制粘贴
2019-03-05
力扣239. 滑动窗口最大值
2019-03-05
史上最全Vue的组件传值
2019-03-05
6.14编一个程序,将两个字符串s1和s2比较,不要用strcmp函数。
2019-03-05
如何解决vscode检测到#include错误,请更新includePath。
2019-03-05
Java纯文本文件显示工具制作
2019-03-05
Unity2D Fixed Joint 2D详解
2019-03-05
Unity Shader之路(五)创建第一个顶点/片元着色器?
2019-03-05
L3-008 喊山 (30分) C++ BFS题解
2019-03-05
Web框架——Flask系列之Flask-SQLAlchemy数据库的基本操作(九)
2019-03-05
六、Numpy的使用(详解)
2019-03-05
三、案例:留言板 & url.parse()
2019-03-05
Python中的filter()函数!!!1
2019-03-05
(新手小白必学!)用Python设计和实现聪明的尼姆游戏(人机对战)!!!!
2019-03-05
LeetCode:283. 移动零!!!1
2019-03-05
Python实验26:计算文件MD5值
2019-03-05
端口探测
2019-03-05