一起找bug

本文介绍了一个关于Java内部类访问权限的问题及解决方法。错误发生在尝试创建一个内部类实例时,由于没有正确指定外部类的实例,导致编译错误。通过提供错误代码示例和解释,本文帮助读者理解如何避免此类问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

帮同学找的一个bug,错误代码如下:

 1 package dai_test;
 2 public class Test1 {
 3 
 4     public static void main(String[] args) {
 5         // TODO Auto-generated method stub
 6         MyThread myThread = new MyThread();
 7         myThread.setName("dai");
 8         myThread.start();
 9     }
10     class MyThread extends Thread{
11         public void run() {
12             super.run();
13              for(int i =0;i<10;i++){        
14                  System.out.println(  "Main.run()"+i+Thread.currentThread().getName()   );                
15           }
16         }
17     }
18 }

错误信息为第6行:No enclosing instance of type Test1 is accessible. Must qualify the allocation with an enclosing instance of type Test1 (e.g. x.new
 A() where x is an instance of Test1).

错误原因:将class MyThread写到了Class Text1里面。。。

转载于:https://www.cnblogs.com/yidijimao/p/5149599.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值