`

anonymous class

    博客分类:
  • java
阅读更多

anonymous classes

A type of inner class that has no name that you define right in the middle of a method (where static init blocks and instance init blocks count as methods). You define it, and create an object of that type as a parameter all in one line. Used for creating simple delegate callback objects. The syntax is strange. It does not use the keywords class , implements or extends . You can refer to this of the outer class via MyOuterClass . this . You can refer to the outer class’s methods by MyOuterClass . this . myOuterInstanceMethod (), MyOuterClass . myOuterInstanceMethod () or simply myOuterInstanceMethod () if there is no ambiguity. Anonymous inner classes are often used in setting up listeners.

These anonymous inner classes can access the static and instance variables of the enclosing outer class. They can also, believe it or not, access a snapshot of the local variables in the method that created the inner class object. To remind you this you are accessing an immutable snapshot, Java insists that the local variables you reference be final . When the class is created, a copy of all the local variables is bundled along with the object for all future references. So in a sense these classes act like closures, taking a little of the environment in which they were created along with them.

Instead of passing arguments to a constructor, your inner class methods can reach out and grab what they need directly from local variables in the enclosing method. The other technique is to use an instance initialiser block. You are only allowed one per anonymous inner class.

The requirement that the enclosing local variables that you access in your anonymous inner class be final is not quite as onerous as it first seems. Just create a new block and create new final local variables in it that copy the non-final ones of the outer block. You can also declare a local variable final inside a loop so long as the value does not change for the rest of the current iteration, even though it will change on the next iteration.

The inner class object may live beyond that of the method that invoked it, so you could not very well have the inner class modifying local variables in the caller that no longer exist. That is why you may only access final local variables. I repeat, you are not really accessing the local variables but a snapshot of them taken at the time the inner class object was instantiated. Anonymous classes are one of the bailing wire constructs in Java .

If you want to baffle those maintaining your code, wags have discovered javac.exe will permit anonymous classes inside static init code and static methods, even though the language spec says than anonymous classes are never static . These anonymous classes, of course, have no access to the instance fields of the object. I don’t recommend doing this. The feature could be pulled at any time.

The big drawback with anonymous classes is they can’t have explicit constructors. You can’t pass them any parameters when they are instantiated. You must rely on the somewhat goofy access to the frozen values of the final temporaries in the invoking method.

Typical Use

分享到:
评论

相关推荐

    AnonymousClass:匿名类

    例如,`AnonymousClass-addSecondButton`可能表示在创建一个新的按钮时,通过匿名类添加了一个事件监听器,监听用户点击按钮的行为。 6. **匿名类与lambda表达式** 自Java 8起,匿名类与lambda表达式相辅相成。当...

    Anonymous inner class1---马克-to-win java视频

    匿名内部类 inner class 马克-to-win java视频的详细介绍

    java易混淆概念Anonymous Inner Class

    Java 易混淆概念 Anonymous Inner Class Java编程语言中存在一些易混淆的概念,今天我们将探讨 Anonymous Inner Class、final、finally、finalize 的区别,Static Nested Class 和 Inner Class 的不同,&和&&的区别...

    Android开发导入项目报错Ignoring InnerClasses attribute for an anonymous inner class的解决办法

    在Android开发过程中,有时会遇到导入项目时出现错误警告,比如"Ignoring InnerClasses attribute for an anonymous inner class"。这个问题并不会阻止项目在Windows系统上运行,但可能会导致在其他平台如OS X上无法...

    本体Protege4

    Named Class是具有语义的类,Anonymous Class是没有语义的类。用户可以使用Protege4.0中的各种功能,例如Class Axiom、Class Expression等来创建和管理本体。 本教程还提供了一个实践例子,指导用户如何打开 ...

    eclipse 格式化模板

    Use anonymous class creations Remove unused imports Add missing '@Override' annotations Add missing '@Override' annotations to implementations of interface methods Add missing '@Deprecated' ...

    java 内部类 局部内部类 匿名类 实例代码

    System.out.println("Message from anonymous class."); } }); } // 输出:Message from anonymous class. } ``` 4. **静态内部类**:静态内部类与实例内部类不同,它不需要外部类的实例就可以被创建。静态内部...

    Java专业术语 英语词汇.doc

    * Anonymous inner class:匿名内部类,指的是在另一个类中定义的匿名类。 * Application Programming Interface (API):应用程序接口,指的是软件系统中的一组定义好的接口。 * Array:数组,指的是一组相同类型的...

    JAVA英文常用词汇.doc

    3. **Anonymous class**:没有名称的类,通常用于创建临时的、一次性的实现,尤其是作为参数传递给方法。 4. **Anonymous inner class**:匿名内部类,是嵌套在另一个类中的无名类,常用来实现接口或继承其他类。 ...

    Java语言常用的方法名.pdf

    Anonymous class,即匿名类,当需要创建和使用一个类,但不需要给出它的名字或再次使用时,可以使用匿名类。Anonymous inner classes,即匿名内部类,是没有类名的局部内部类。 API,即应用程序接口,提供特定功能...

    常用 java 词汇 速查表

    4. **匿名类 (Anonymous class)**:匿名类是在编写代码时没有名称的类,通常用于一次性使用的场合,它可以实现一个接口或继承一个类,但不能同时实现接口和继承类。 5. **匿名内部类 (Anonymous inner class)**:这...

    《面向对象技术与方法》12、大型程序.pdf

    3. **匿名类**(Anonymous Class):没有显式名称的局部类,通常用于实现接口或继承类,并立即创建一个对象。 嵌套类的主要用途在于提供更高级别的封装和组织能力,使得逻辑相关的类可以被紧密地捆绑在一起,同时也...

    JAVA词汇表下载.docJAVA词汇表下载.doc

    5. 匿名类(Anonymous Class):当需要创建一个类但不关心其名称或不再使用时,可以使用匿名类。匿名类可以在单个语句中定义并实例化,无需显式声明类名。 6. 匿名内部类(Anonymous Inner Classes):这是局部内部...

    java日常词汇学习

    5. **匿名内部类 (Anonymous inner class)**:这是没有名称的局部内部类,通常用于实现接口或继承其他类,并且可以在定义时直接初始化。 6. **API (应用程序接口)**:API 是一组预先定义好的类和方法,开发者可以...

    java词汇速查(初级)

    4. **匿名类 (Anonymous class)**:没有名字的类,通常用于一次性创建对象,特别是当只需要实现一个接口或继承一个类时。 5. **匿名内部类 (Anonymous inner class)**:没有名称的局部内部类,常用于实现接口或继承...

    java 词汇表速查手册

    4. Anonymous class 匿名类:匿名类是指不需要给出类名的类。匿名类通常用于一次性使用的场景,例如创建一个临时的类来实现某个接口。 5. Anonymous inner classes 匿名内部类:匿名内部类是指没有类名的局部内部类...

    java考题汇总java考题汇总.doc

    3. 匿名类(Anonymous class)和匿名内部类(Anonymous inner class):当只需要使用一次且不需要命名时,可以创建匿名类。匿名内部类可以直接继承一个类或实现一个接口,常用于简化代码,如事件监听器的实现。 4. ...

Global site tag (gtag.js) - Google Analytics