`
紫竹星云
  • 浏览: 29577 次
社区版块
存档分类
最新评论

Java出现No enclosing instance of type E is accessible. Must qualify the allocatio

 
阅读更多

最近在看Java,在编译写书上一个例子时,由于书上的代码只有一部分,于是就自己补了一个内部类。结果编译时出现:No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing instance of type E(e.g.  x.new A() where x is an instance of E). E指代我写的那个内部类。

根据提示,没有可访问的内部类E的实例,必须分配一个合适的内部类E的实例(如x.new A(),x必须是E的实例。)看着这句提示,我就纳闷了,我已经用new实例化了这个类,为什么还不行呢。

于是百度谷歌了一下相关资料。原来我写的内部类是动态的,也就是开头以public class开头。而主程序是public static class main。在Java中,类中的静态方法不能直接调用动态方法。只有将某个内部类修饰为静态类,然后才能够在静态类中调用该类的成员变量与成员方法。所以解决办法是将public class改为public static class.

在这里记下这个问题,也方面遇到同样问题的朋友查阅。

分享到:
评论

相关推荐

    Java解决No enclosing instance of type PrintListFromTailToHead is accessible问题的两种方案

    Java编程语言中的"No enclosing instance of type"错误通常发生在尝试创建非静态内部类的实例,但没有一个外部类的实例可以访问时。在上述示例中,`ListNode`是`PrintListFromTailToHead`类的非静态内部类。由于`...

    java编译中存在的问题及原因分析

    Must qualify the allocation with an enclosing instance of type EmployeeTest (e.g. x.new A() where x is an instance of EmployeeTest).” **原因分析:** 此错误是因为在Java中,非静态内部类(也称为成员...

    2009 达内Unix学习笔记

    -e 所有(every),比a更详细。 -f 取消保护。 -i 添加提示。 -p 强制执行。 -r 目录管理。 分屏显示的中途操作 空格<space> 继续打开下一屏; 回车<return> 继续打开下一行; b 另外开上一屏; f 另外开...

    Google C++ Style Guide(Google C++编程规范)高清PDF

    On the other hand, you must include the header file for Foo if your class subclasses Foo or has a data member of type Foo. Sometimes it makes sense to have pointer (or better, scoped_ptr) members ...

    Secrets of the JavaScript Ninja, 2nd Edition

    Generators are a special type of function that can pause and resume execution, allowing the generation of values on demand. They use the `yield` keyword to produce a sequence of values. For instance: ...

    Mask 98 for PRwin98

    is just a sample of the Windows 95/98 type enhancements Calmira provides to the Windows 3.1 user interface... Taskbar with versatile Start Menu, nested to any level Wastepaper bin to store deleted ...

    max200技术手册

    数控等离子切割技术资料The user is responsible for installing and using the plasma equipment according to the manufacturer’s instructions. If electromagnetic disturbances are detected then it shall be...

    Using LUA with Visual C++ (Introduction)

    The first parameter is the pointer to the LUA state, the second parameter is the number of arguments on the stack that the command takes (and when executing a loaded script this can remain 0)....

    The Java Module System

    Java模块系统是Java平台标准版(Java SE)9及更高版本引入的一项重要特性,它旨在解决大型应用程序的可维护性、可伸缩性和安全性问题。这个系统,也被称为Project Jigsaw,通过提供一种结构化的方式来组织和管理Java...

    embedded system design

    products in the area of consumer electronics the amount of code is doubling every two years [Vaandrager, 1998]. This importance of embedded systems is so far not well reflected inmany of the current ...

    SCJP6 Sun Certificated Programmer for Java 6 Study Guide (Exam 310-065) 英文原版

    Inner classes can access private members of the enclosing class: - **Member Inner Classes**: Declared inside a class but outside any method. - **Local Inner Classes**: Declared inside a method. - **...

    Borland Delphi 2005 Architect Update 3

    F2084 Internal Error SCR765.00 of 5 Closed11193 Inlined function affects working of the enclosing for loop0.00 of 5 Closed11184 [Fatal Error] F2084 Internal Error: ILLK26340.00 of 5 Closed11069 ...

    Android Studio无法执行Java类的main方法问题及解决方法

    Android Studio升级到哦最新版3.6.1后,新建了个项目,发现无法执行Java类的main方法。试了网上的各种方法,比如切换gradle离线模式、gradle.properties中添加android.enableAapt2=false等,我还尝试了重新情况...

    Fractions to Decimals

    Write a program that will accept a fraction of the form N/D, where N is the numerator and D is the denominator and print the decimal representation. If the decimal representation has a repeating ...

    Android 中LayoutInflater.inflate()方法的介绍

    Android 中LayoutInflater.inflate()方法的介绍 Android 中LayoutInflater.inflate()方法是Android开发中最常用的方法之一,用于将布局文件转换成View对象。该方法是LayoutInflater类中的一个成员方法,主要用于将...

    Shell Script

    Here, `date` inside backticks is executed first to get the current date, which is then printed as part of the sentence. #### Arithmetic Operations in Shell Scripts Shell scripts can perform ...

    Eclipse Template与模式

    private static final ${enclosing_type} m_instance = new ${enclosing_type}(); /** * 私有的默认构造子 */ private ${enclosing_type}() {} /** * 静态工厂方法 */ public static ${enclosing_type}...

    Java的注释

    ### Java的注释 在Java开发中,良好的注释习惯对于提高代码的可读性和可维护性至关重要。本文将详细介绍几种常见的Java注释方式及其用途,并通过实例展示如何设置Eclipse IDE中的注释模板,帮助开发者更好地管理和...

    java文件头注释格式[文].pdf

    Java文件头注释格式是软件开发中的一种良好实践,它有助于提高代码的可读性和维护性。在Eclipse这样的集成开发环境中,可以方便地设置和自定义这些注释模板。以下是一些关于Java文件头注释格式的关键知识点: 1. **...

Global site tag (gtag.js) - Google Analytics