- 浏览: 417323 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
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 829我记得很早以前在网上看过文章,大致讲axis2的质量如何 ... -
使用java连接https的问题
2012-03-19 15:41 809在使用hudson的过程中,我们需要发送邮件的功能。但是公司的 ... -
SSL exception: "No subject alternative names matching IP address ..." & "No name
2012-02-08 09:35 10396When you want to establish an S ... -
ResourceBundle加载文件的顺序
2011-12-24 15:01 1318If a ResourceBundle class for ... -
异常处理框架
2011-09-07 14:57 785The Nature of Exceptions Bro ... -
httpclient的并发连接问题
2011-05-24 16:14 6653昨天的搜索系统又出状况了,几个库同时重建索引变得死慢。经 ... -
java connect https
2011-05-04 15:37 950When I use java to connect HTTP ... -
jvm的高性能
2011-04-25 13:48 776jdk将源代码编译成字节码之后,由JVM在运行期对其进行解释执 ... -
java annotation
2011-03-10 14:58 851JDK内置的annotaion 1. @Target ... -
java字节码的操纵
2011-03-09 16:35 1486http://www.infoq.com/cn/article ... -
java 范型
2011-03-09 15:15 704Java泛型(generics)是JDK 5中引入的一个新特性 ... -
正确使用 Volatile 变量
2011-03-04 09:34 746Java 语言中的 volatile 变 ... -
java中Thread与Runnable的区别
2011-02-25 20:42 1697在java中可有两种方式实现多线程,一种是继承Thread类, ... -
copy-on-write
2010-12-08 10:29 894Copy-on-write (sometimes refe ... -
synchronized原理
2010-11-29 14:40 1812每个JAVA对象都有一把锁, 当有多个线程同时访问共享资源的时 ... -
Map 四种同步方式的性能比较
2010-11-25 11:50 934如果需要使 Map 线程安全,大致有这么四种方法: 1、 ... -
Java中HashMap,LinkedHashMap,TreeMap的区别
2010-11-25 11:49 1712java为数据结构中的映射 ... -
java的内存泄漏
2010-11-25 10:52 10101 引言 Java的一个重要优点就是通过垃圾收集器 ... -
解析Java对象的equals()和hashCode()的使用
2010-11-25 10:49 1051前言 在Java语言中,equals()和hash ... -
java map
2010-11-25 10:40 10751. Map key: 同一个key必须hashcode相同。 ...
相关推荐
AnonymousClass:匿名类
匿名内部类 inner class 马克-to-win java视频的详细介绍
java易混淆的地方,final, finally, finalize的区别,Anonymous Inner Class (匿名内部类) 是否可以extends(继承)其它类
Anonymous class 匿名类:当你需要创建和使用一个类,而又不需要给出它的名字或者再次使用的使用,就可以利用匿名类。 Anonymous inner classes 匿名内部类:匿名内部类是没有类名的局部内部类。 API 应用...
Use anonymous class creations Remove unused imports Add missing '@Override' annotations Add missing '@Override' annotations to implementations of interface methods Add missing '@Deprecated' ...
1.Nested Class(嵌套类) 1.1.Nested class 1.2.Nested class的分类 1.3.Nested class的使用原因 2.Static Nested Classes ...3.2.Anonymous Inner Class(Inner Class) 3.3.Local Inner Class(Inner Class)
10.4 匿名类(anonymous class) 152 10.5 巩固练习 153 第11章 异常处理(exception handing) 154 11.1 try-catch语句 154 11.2 Exception类的种类 157 11.3 可抛出异常的方法 158 11.4 自定义异常 161...
匿名内部类 inner class 马克-to-win java视频的详细介绍
很好的Java面试题第二,Anonymous Inner Class (匿名内部类) 是否可以extends(继承)其它类,是否可以implements(实现)interface(接口)?
AnonymousInnerClass
今天小编就为大家分享一篇关于Android开发导入项目报错Ignoring InnerClasses attribute for an anonymous inner class的解决办法,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随...
第二,Anonymous Inner Class (匿名内部类) 是否可以extends(继承)其它类,是否可以implements(实现)interface(接口)? 第三,Static Nested Class 和 Inner Class的不同,说得越多越好(面试题有的很笼统)。 第四...
第二,Anonymous Inner Class (匿名内部类) 是否可以extends(继承)其它类,是否可以implements(实现)interface(接口)? 第三,Static Nested Class 和 Inner Class的不同,说得越多越好(面试题有的很笼统)。 第四,&...
final—修饰符 Anonymous Inner Class Static Nested Class 和 Inner Class的不同
第二,Anonymous Inner Class (匿名内部类) 是否可以extends(继承)其它类,是否可以implements(实现)interface(接口)? 第三,Static Nested Class 和 Inner Class的不同,说得越多越好(面试题有的很笼统)。 ...
第二,Anonymous Inner Class (匿名内部类) 是否可以extends(继承)其它类,是否可以implements(实现)interface(接口)? 第三,Static Nested Class 和 Inner Class的不同,说得越多越好(面试题有的很笼统)。 第四,&...
第二,Anonymous Inner Class (匿名内部类) 是否可以extends(继承)其它类,是否可以implements(实现)interface(接口)? 第三,Static Nested Class 和 Inner Class的不同,说得越多越好(面试题有的很笼统)。 第四,...
棉花糖数据类 从数据类自动生成模式。 from dataclasses import dataclass , field from typing import List , Optional import marshmallow_dataclass ... name : str = field ( default = "anonymous" )