A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© hanrongle 中级黑马   /  2013-7-10 15:03  /  906 人查看  /  13 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 杨兴庭 于 2013-7-10 22:39 编辑

 在创建非静态内部类时,经常会遇到“No enclosing instance of type * is accessible. Must qualify the allocation with an enclosing   instance of type *(e.g. x.new A() where x is an instance of *).”这样的报错误,怎么处理?

评分

参与人数 1黑马币 +3 收起 理由
杨兴庭 + 3

查看全部评分

13 个回复

倒序浏览
最好能把源代码贴出来 让大伙儿看看帮你找问题,光给个错误提示不好找原因。
回复 使用道具 举报
  package com.csc.innerclasstest;
  /**
  *
  * @author csc
  *
  */
  //外部类
  public class OuterClass {
  /**
  * @param args
  */
  public static void main(String[] args) {
  InnerClass innerClass = new InnerClass();
  innerClass.say();
  System.out.println("I am in OuterClass!");
  }
  //定义一个内部类
  private class InnerClass{
  private void say() {
  System.out.println("I am in InnerClass!");
  }
  }
  }
  上面的代码的第16行将会报出“No enclosing instance of type OuterClass is accessible. Must qualify the allocation with an enclosing instance of type OuterClass (e.g. x.new A() where x is an instance of OuterClass).”这样的编译错误。

怎么处理?
回复 使用道具 举报
外部类名。内部类名
new wai().new nei()
回复 使用道具 举报 1 0
你的16行是从哪里开始查的,能不能把代码写到代码的模式中
回复 使用道具 举报
static class InnerClass
回复 使用道具 举报
majunm 中级黑马 2013-7-10 15:28:10
7#
hanrongle 发表于 2013-7-10 15:09
  package com.csc.innerclasstest;
  /**
  *

OutClass.InnerClass    in=new OutClass().new InnerCass();
in.say();

评分

参与人数 1技术分 +1 收起 理由
杨兴庭 + 1

查看全部评分

回复 使用道具 举报
谢威 中级黑马 2013-7-10 15:28:39
8#
  1. class OuterClass {
  2.         public static void main(String[] args) {
  3.                 //java中静态方法中不能调用非静态的方法
  4.                 InnerClass innerClass = new InnerClass();
  5.                 innerClass.say();
  6.                 System.out.println("I am in OuterClass!");
  7.         }

  8.         //定义一个内部类
  9.         // private class InnerClass{ 将这里添加一个static  
  10.     private static class InnerClass{
  11.                 private void say() {
  12.                         System.out.println("I am in InnerClass!");
  13.                 }
  14.         }
  15. }
复制代码

点评

这样改跟楼主原来的思想有冲突,楼主想定义一个非静态的内部类,这样改的话就事与愿违了  发表于 2013-7-10 15:50
回复 使用道具 举报
majunm 中级黑马 2013-7-10 15:29:48
9#
本帖最后由 majunm 于 2013-7-10 15:31 编辑

OutClass.InnerClass    in=new OutClass().new InnerCass();
in.say();

试试 行不行 兄弟!
回复 使用道具 举报
majunm 发表于 2013-7-10 15:29
OutClass.InnerClass    in=new OutClass().new InnerCass();
in.say();
试试 行不行 兄弟!

            好了!
回复 使用道具 举报
majunm 发表于 2013-7-10 15:29
OutClass.InnerClass    in=new OutClass().new InnerCass();
in.say();
试试 行不行 兄弟!

            好 了!
回复 使用道具 举报
camml 中级黑马 2013-7-10 15:45:45
12#
本帖最后由 camml 于 2013-7-10 15:47 编辑

楼主的代码很明显会有编译错误,如果外部类的静态方法想创建非静态内部类的对象时,需要加上外部类的类名。
下面是修改后的代码
  1. public class OuterClass  {
  2.         public static void main(String[] args) {
  3.                 //原代码会出现“java中静态方法中不能调用非静态的方法”的错误,修改为
  4.                 OuterClass.InnerClass innerClass = new OuterClass().new InnerClass();
  5.                 innerClass.say();
  6.                 System.out.println("I am in OuterClass!");
  7.         }

  8.         //定义一个内部类
  9.        private  class InnerClass{
  10.                 private void say() {
  11.                         System.out.println("I am in InnerClass!");
  12.                 }
  13.         }
  14. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
杨兴庭 + 1

查看全部评分

回复 使用道具 举报
camml 中级黑马 2013-7-10 15:48:53
13#
至于楼主所遇到的错误,真心没有遇到,请楼主重新打一次代码,看是否会有这样的报错提示,希望对楼主有帮助
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马