Java编写代码过程中遇到了一个问题,main方法中创建内部类的实例时,编译阶段出现错误,查看错误描述:

No enclosing instance of type Test is accessible. Must qualify the allocation with an enclosing instance of type Test (e.g. x.new A() where x is an instance of Test).

经过查证,是因为内部类是动态的(无static关键字修饰),而main方法是静态的,普通的内部类对象隐含地保存了一个引用,指向创建它的外围类对象,所以要在static方法(类加载时已经初始化)调用内部类的必须先创建外部类。即应该这样创建“DanymicTest test = new StaticCallDynamic().new DanymicTest();”其中StaticCallDynamic为外部类,DanymicTest为内部动态类;如果将内部内修改为静态类,可以在main中直接创建内部类实例。

  顺便看了下静态方法对动态方法的调用,如果在静态方法中直接调用静态方法,其中print()为类中的动态方法,会出现如下问题:

Cannot make a static reference to the non-static method print() from the type StaticCallDynamic

  这个要从java的内存机制去分析,首先当你New 一个对象的时候,并不是先在堆中为对象开辟内存空间,而是先将类中的静态方法(带有static修饰的静态函数)的代码加载到一个叫做方法区的地方,然后再在堆内存中创建对象。所以说静态方法会随着类的加载而被加载。当你new一个对象时,该对象存在于对内存中,this关键字一般指该对象,但是如果没有new对象,而是通过类名调用该类的静态方法也可以。

  程序最终都是在内存中执行,变量只有在内存中占有一席之地时才会被访问,类的静态成员(变态和方法)属于类本身,在类加载的时候就会分配内存,可以通过类名直接去访问,非静态成员(变量和方法)属于类的对象,所以只有在类的对象产生(创建实例)的时候才会分配内存,然后通过类的对象去访问。在一个类的静态成员中去访问非静态成员之所以会出错是因为在类的非静态成员不存在的时候静态成员就已经存在了,访问一个内存中不存在的东西当然会出错。

  就类中的静态方法不能调用动态方法,所以要么将动态方法也改为静态方法(static),要么通过创建该静态方法所在的实例(new 静态c)再通过实例(c.d动态)来调用。

No enclosing instance of type Test is accessible. Must qualify the allocation with an enclosing instance of type Test (e.g. x.new A() where x is an instance of Test).的更多相关文章

  1. 【转】Java出现No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing

    最近在看Java,在编译写书上一个例子时,由于书上的代码只有一部分,于是就自己补了一个内部类.结果编译时出现:No enclosing instance of type E is accessible ...

  2. Java出现No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing

    Java出现No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing   ...

  3. No enclosing instance of type test8 is accessible. Must qualify the allocation with an enclosing instance of type test8 (e.g. x.new A() where x is an

    在编译一个例子时,结果编译时出现: No enclosing instance of type test8 is accessible. Must qualify the allocation wit ...

  4. No enclosing instance of type Outer is accessible. Must qualify the allocation with an enclosing instance of type Outer (e.g. x.new A() where x is an instance of Outer)

    之前看内部类的时候没发现这个问题,今天写代码的时候遇到,写个最简单的例子: 下面这一段代码 红色的部分就是编译报错: No enclosing instance of type Outer is ac ...

  5. No enclosing instance of type Demo is accessible. Must qualify the allocation with an enclosing instance of type Demo (e.g. x.new A() where x is an instance of Demo).

    No enclosing instance of type Demo is accessible. Must qualify the allocation with an enclosing inst ...

  6. 【eclipse】No enclosing instance of type A is accessible. Must qualify the allocation with an enclosing instance of type A

    用 eclipse 写 Java 代码时出现了这个问题,详细如下: No enclosing instance of type TestParsingLinkedList is accessible. ...

  7. No enclosing instance of type test is accessible. Must qualify the allocation with an enclosing inst

    今日遇到一个报错如下: No enclosing instance of type test is accessible. Must qualify the allocation with an en ...

  8. No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing

    在Java中,类中的静态方法不能直接调用动态方法.只有将某个内部类修饰为静态类,然后才能够在静态类中调用该类的成员变量与成员方法.所以在不做其他变动的情况下,最简单的解决办法是将public clas ...

  9. Java中报错No enclosing instance of type caiquan is accessible. Must qualify the allocation with an enclosing instance of type caiquan (e.g. x.new A() where x is an instance of caiquan).

    package test;import java.util.Scanner;import java.util.Random;public class caiquan { public static v ...

随机推荐

  1. OpenLayers学习笔记(十一)— 飞机速度矢量线预测

    在机场使用的空管系统中,飞机的速度矢量线差不多是这样的: 模拟飞机飞行时的速度矢量线,这里就大概做个类似效果: 什么叫速度矢量线呢,个人理解就是根据飞机当前速度和航向预测它在未来一段时间内的飞机轨迹, ...

  2. pytest 13 使用自定义标记mark

    前言: pytest可以规定那些要跑,那些不跑,跑特定的哪些?比如以下的这个例子: #!/usr/bin/env/python # -*-coding:utf-8-*- import pytest @ ...

  3. Java基础知识拾遗(二)

    Lambda表达式 lambda表达式本质上就是一个匿名方法.但是这个方法不是独立执行的,而是构成了一个函数式接口定义的抽象方法的实现,该函数式接口定义了它的目标类型. 只有在定义了lambda表达式 ...

  4. 使用Docker+Jenkins自动构建部署

    环境 Windows 10 Docker Version 18.06.1-ce-win73 (19507) 运行jenkins 运行jenkins 容器 docker run -d --name ln ...

  5. HBase基本概念与基本使用

    1. HBase简介 1.1 什么是HBase HBASE是一个高可靠性.高性能.面向列.可伸缩的分布式存储系统,利用HBASE技术可在廉价PC Server上搭建起大规模结构化存储集群. HBASE ...

  6. 试用VS2019正式版

    1.下载地址https://visualstudio.microsoft.com/zh-hans/downloads/2.安装,安装过程比较简单,直接下一步,下一步即可,现在的网速一般下载+安装要2个 ...

  7. swift 4 生成随机数的内置方法汇总

    第一种是drand48(),不接收参数, 返回的类型是Double. 就返回 0到1之间的Double类型的随机数.举个例子: //每次点击button,button 的颜色会随机变换. class ...

  8. node-RED

    node-RED提供了一个基于浏览器的编辑器,可以轻松地使用调色板中的广泛节点将流连接在一起,这些节点可以通过单击部署到其运行时.使用Node-RED,开发人员将输入/输出和处理节点连接起来,创建流程 ...

  9. Hadoop yarn任务调度策略介绍(转)

    理想情况下,我们应用对Yarn资源的请求应该立刻得到满足,但现实情况资源往往是有限的,特别是在一个很繁忙的集群,一个应用资源的请求经常需要等待一段时间才能的到相应的资源.在Yarn中,负责给应用分配资 ...

  10. IO流的操作规律。

    1.  明确源和目的 源代表输入流: InputStream, Reader 目的代表输出流: OutputStream, Writer 2. 操作数据是否纯文本 纯文本:字符流 非纯文本: 字节流 ...