---
title: "Java编译错误“No enclosing instance of type"
author: "abc20899"
date: "2012-06-15T16:14:45Z"
tags: []
categories: ["移动开发"]
slug: "java-no-enclosing-instance-of-type"
draft: false
lastmod: "2012-06-15T16:14:45Z"
id: 1561691
url: "https://abc20899.iteye.com/blog/1561691"
---

Java编译错误“No enclosing instance of type AA is accessible. Must qualify the allocation with an enclosing instance of type SimpleTh


这是因为AA是一个动态的内部类，创建这样的对象必须有实例与之对应，程序是在静态方法中直接调用动态内部类会报这样错误。   这样的错误好比类中的静态方法不能直接调用动态方法。可以把该内部类声明为static。或者不要在静态方法中调用。

那么为啥非静态方法不能调用动态方法呢，从面向对象的角度来说，动态方法与对象是联系密切的，比如发动是一个方法，它与汽车这个对象是关联的，所以只有new了汽车这个对象才能执行汽车.发动