- 浏览: 425066 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
burningblood:
最近也遇到了这个细节问题。我用的是4,里面没有 get.rel ...
httpclient的并发连接问题 -
greatwqs:
使用HttpURLConnection注意设置超时 -
qinweilh:
...
tomcat报错:standardServer.await: create[8005]: -
jayyunfei:
还是不很明白
JPA entityManager的管理 -
a418040445:
...
Calendar
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
发表评论
-
糟糕透顶的axis2
2015-02-06 15:27 856我记得很早以前在网上看过文章,大致讲axis2的质量如何 ... -
使用java连接https的问题
2012-03-19 15:41 844在使用hudson的过程中,我们需要发送邮件的功能。但是公司的 ... -
SSL exception: "No subject alternative names matching IP address ..." & "No name
2012-02-08 09:35 10683When you want to establish an S ... -
ResourceBundle加载文件的顺序
2011-12-24 15:01 1362If a ResourceBundle class for ... -
异常处理框架
2011-09-07 14:57 818The Nature of Exceptions Bro ... -
httpclient的并发连接问题
2011-05-24 16:14 6683昨天的搜索系统又出状况了,几个库同时重建索引变得死慢。经 ... -
java connect https
2011-05-04 15:37 988When I use java to connect HTTP ... -
jvm的高性能
2011-04-25 13:48 806jdk将源代码编译成字节码之后,由JVM在运行期对其进行解释执 ... -
java annotation
2011-03-10 14:58 897JDK内置的annotaion 1. @Target ... -
java字节码的操纵
2011-03-09 16:35 1527http://www.infoq.com/cn/article ... -
java 范型
2011-03-09 15:15 752Java泛型(generics)是JDK 5中引入的一个新特性 ... -
正确使用 Volatile 变量
2011-03-04 09:34 786Java 语言中的 volatile 变 ... -
java中Thread与Runnable的区别
2011-02-25 20:42 1742在java中可有两种方式实现多线程,一种是继承Thread类, ... -
copy-on-write
2010-12-08 10:29 926Copy-on-write (sometimes refe ... -
synchronized原理
2010-11-29 14:40 1855每个JAVA对象都有一把锁, 当有多个线程同时访问共享资源的时 ... -
Map 四种同步方式的性能比较
2010-11-25 11:50 969如果需要使 Map 线程安全,大致有这么四种方法: 1、 ... -
Java中HashMap,LinkedHashMap,TreeMap的区别
2010-11-25 11:49 1743java为数据结构中的映射 ... -
java的内存泄漏
2010-11-25 10:52 10441 引言 Java的一个重要优点就是通过垃圾收集器 ... -
解析Java对象的equals()和hashCode()的使用
2010-11-25 10:49 1082前言 在Java语言中,equals()和hash ... -
java map
2010-11-25 10:40 11331. Map key: 同一个key必须hashcode相同。 ...
相关推荐
例如,`AnonymousClass-addSecondButton`可能表示在创建一个新的按钮时,通过匿名类添加了一个事件监听器,监听用户点击按钮的行为。 6. **匿名类与lambda表达式** 自Java 8起,匿名类与lambda表达式相辅相成。当...
匿名内部类 inner class 马克-to-win java视频的详细介绍
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"。这个问题并不会阻止项目在Windows系统上运行,但可能会导致在其他平台如OS X上无法...
Named Class是具有语义的类,Anonymous Class是没有语义的类。用户可以使用Protege4.0中的各种功能,例如Class Axiom、Class Expression等来创建和管理本体。 本教程还提供了一个实践例子,指导用户如何打开 ...
Use anonymous class creations Remove unused imports Add missing '@Override' annotations Add missing '@Override' annotations to implementations of interface methods Add missing '@Deprecated' ...
System.out.println("Message from anonymous class."); } }); } // 输出:Message from anonymous class. } ``` 4. **静态内部类**:静态内部类与实例内部类不同,它不需要外部类的实例就可以被创建。静态内部...
* Anonymous inner class:匿名内部类,指的是在另一个类中定义的匿名类。 * Application Programming Interface (API):应用程序接口,指的是软件系统中的一组定义好的接口。 * Array:数组,指的是一组相同类型的...
3. **Anonymous class**:没有名称的类,通常用于创建临时的、一次性的实现,尤其是作为参数传递给方法。 4. **Anonymous inner class**:匿名内部类,是嵌套在另一个类中的无名类,常用来实现接口或继承其他类。 ...
Anonymous class,即匿名类,当需要创建和使用一个类,但不需要给出它的名字或再次使用时,可以使用匿名类。Anonymous inner classes,即匿名内部类,是没有类名的局部内部类。 API,即应用程序接口,提供特定功能...
4. **匿名类 (Anonymous class)**:匿名类是在编写代码时没有名称的类,通常用于一次性使用的场合,它可以实现一个接口或继承一个类,但不能同时实现接口和继承类。 5. **匿名内部类 (Anonymous inner class)**:这...
3. **匿名类**(Anonymous Class):没有显式名称的局部类,通常用于实现接口或继承类,并立即创建一个对象。 嵌套类的主要用途在于提供更高级别的封装和组织能力,使得逻辑相关的类可以被紧密地捆绑在一起,同时也...
5. 匿名类(Anonymous Class):当需要创建一个类但不关心其名称或不再使用时,可以使用匿名类。匿名类可以在单个语句中定义并实例化,无需显式声明类名。 6. 匿名内部类(Anonymous Inner Classes):这是局部内部...
5. **匿名内部类 (Anonymous inner class)**:这是没有名称的局部内部类,通常用于实现接口或继承其他类,并且可以在定义时直接初始化。 6. **API (应用程序接口)**:API 是一组预先定义好的类和方法,开发者可以...
4. **匿名类 (Anonymous class)**:没有名字的类,通常用于一次性创建对象,特别是当只需要实现一个接口或继承一个类时。 5. **匿名内部类 (Anonymous inner class)**:没有名称的局部内部类,常用于实现接口或继承...
4. Anonymous class 匿名类:匿名类是指不需要给出类名的类。匿名类通常用于一次性使用的场景,例如创建一个临时的类来实现某个接口。 5. Anonymous inner classes 匿名内部类:匿名内部类是指没有类名的局部内部类...
3. 匿名类(Anonymous class)和匿名内部类(Anonymous inner class):当只需要使用一次且不需要命名时,可以创建匿名类。匿名内部类可以直接继承一个类或实现一个接口,常用于简化代码,如事件监听器的实现。 4. ...