社区
Java SE
帖子详情
请问:inner class中使用的参数为什么必须为final啊?
hanxiao19811230
2007-03-27 11:02:22
interface A {
void f();
}
class B {
public A m(final int a) {
return new A() {
int b = a;
void f() {};
}
}
}
请问 final int a, 为什么必须是final
...全文
343
9
打赏
收藏
请问:inner class中使用的参数为什么必须为final啊?
interface A { void f(); } class B { public A m(final int a) { return new A() { int b = a; void f() {}; } } } 请问 final int a, 为什么必须是final
复制链接
扫一扫
分享
转发到动态
举报
AI
作业
写回复
配置赞助广告
用AI写文章
9 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
shengli_liao
2007-03-28
打赏
举报
回复
xuexi....
qzjackie
2007-03-28
打赏
举报
回复
是的。你也可以自己敲一下。代码。试一下。就清楚了。
class Gizmo{
public void sppin(){}
}
public class FinalArguments{
void with(final Gizmo g){
//不能 g=new Gizmo();
}
void without(Gizmo g){
g=new Gizmo();//这里就是可以的哦。
}
}
kingofworl
2007-03-28
打赏
举报
回复
一种规范 怕乱引用
hanxiao19811230
2007-03-28
打赏
举报
回复
还是不太明白
maquan
2007-03-28
打赏
举报
回复
to yiyi2007():
大概是文字翻译方面的问题。在《Java Language Specification》中有两个词,nested class 和 inner class,是有区别的。
楼主说的应该是 inner class,而你上面说的这个“内部类”好像是指 nested class。
yiyi2007
2007-03-28
打赏
举报
回复
二楼说“1.内部类不允许静态修饰(包括静态域,方法,静态初始化)”
有点问题。
内部类可以使用public,protected,default,private等4种访问权限控制
内部类可以被声明为static(普通类是不可以的),这样的内部类变成顶层类,相当于把它放在外面,不再是嵌套的内部类,不能再引用外包类对象。
顶层类可以声明static成员。如果内部类需要定义static成员,则该内部类必须声明为static,否则,一般内部类的成员不能被声明为static.
以上文字是书中的原话。
yiyi2007
2007-03-28
打赏
举报
回复
楼主的代码,我敲到MyEclipse中,编译的通不过,在B类中的f(){}这句会出现红色波浪线。
而且楼主还将代码写成 void f() {}; 这里加上分号本来就是错的
www203
2007-03-27
打赏
举报
回复
内部类使用注意事项:
这里不考虑静态内部类,因为没有什么特别的限制
1.内部类不允许静态修饰(包括静态域,方法,静态初始化)
2.内部类可以直接访问外围类的方法。
3.内部类可以直接访问外围类的成员变量,语法为:OuterClass.this.FIELDNAME。
4.如果要访问方法的参数,必须为final,主要针对匿名类和临时内部类而已,因为他们定义
在方法体内。这里把方法体的临时变量作为一个延生,也是必须为fianl的。这样做是为了规范内部类的行为.
这是出于变量作用域的限制考虑. 内部类能使用的变量局限于内部类里面声明的变量, 一般情况下不能使用外面的. 但如果外部变量被声明为final的, 就相当于是一个全局变量, 它的生命周期就扩展到内部类里面了, 因此可以访问.
加final修饰符是为了防止该变量指向其他引用,比如你将这个变量指向null而外部类却毫不知情的继续在使用这个null的变量
frilly
2007-03-27
打赏
举报
回复
内部类使用注意事项:
这里不考虑静态内部类,因为没有什么特别的限制
1.内部类不允许静态修饰(包括静态域,方法,静态初始化)
2.内部类可以直接访问外围类的方法。
3.内部类可以直接访问外围类的成员变量,语法为:OuterClass.this.FIELDNAME。
4.如果要访问方法的参数,必须为final,主要针对匿名类和临时内部类而已,因为他们定义
在方法体内。这里把方法体的临时变量作为一个延生,也是必须为fianl的。这样做是为了规范内部类的行为.
这是出于变量作用域的限制考虑. 内部类能使用的变量局限于内部类里面声明的变量, 一般情况下不能使用外面的. 但如果外部变量被声明为final的, 就相当于是一个全局变量, 它的生命周期就扩展到内部类里面了, 因此可以访问.
加final修饰符是为了防止该变量指向其他引用,比如你将这个变量指向null而外部类却毫不知情的继续在使用这个null的变量
java反编译工具jad 1.5.8g(可以反编译jdk1.5,1.6)
java反编译工具jad 1.5.8g支持 jdk1.5,jdk1.6。说明很多记住一个万能的命令基本就够用了。jad -sjava -r -8 -o **\*.
class
---------------This is README file for Jad - the fast Java Decompiler.Jad home page: http://www.kpdus.com/jad.htmlCopyright 2001 Pavel Kouznetsov (jad@kpdus.com).0. Please read the disclaimer on the Jad home page.1. Installation.Unzip jad.zip file into any appropriate directory on your hard drive.This will create two files: - an executable file named 'jad.exe' (Windows *) or 'jad' (*n*x) - this README fileNo other setup is required.2. How to use JadTo decompile a single JAVA
class
file 'example1.
class
' type the following: jad example1.
class
This command creates file 'example1.jad' in the current directory.If such file already exists Jad asks whether you want to overwrite it or not.Option -o permits overwriting without a confirmation.You can omit .
class
extension and/or use wildcards in the names ofinput files.Option -s allows to change output file extension: jad -sjava example1.
class
This command creates file 'example1.java'. Be careful when usingoptions -o and -sjava together, because Jad can accidentally overwriteyour own source files.Jad uses JAVA
class
name as an output file name. For example, if
class
file 'example1.
class
' contains JAVA
class
'test1' then Jad will createfile 'test1.jad' rather than 'example1.jad'. If you want to specifyyour own output file name use the output redirection: jad -p example1.
class
> myexm1.javaOption -d allows you to specify another directory for output files,which are created, by default, in the current directory. For example: jad -o -dtest -sjava *.
class
(or jad -o -d test -s java *.
class
, which has the same effect)This command decompiles all .
class
files in the current directory <
Variable ‘cal‘ is accessed from within
inner
class
, needs to be declared
final
Variable ‘cal’ is accessed from within
inner
class
, needs to be declared
final
变量在内部类
中
被访问,那么该变量
必须
在外部声明为
final
内部类
中
使用
但未声明的任何局部变量
必须
在内部类的正文之前明确分配。 Java匿名内部类的方法
中
用到的局部变量都
必须
定义为
final
在JVM
中
,内部类不是直接调用方法的
参数
,而是内部类将传进来的
参数
通过自己的构造器备份到了自己的内部,自己内部的方法调用的实际是自己的属性而不是外部类
关于Android开发
中
某个
参数
遇到了
inner
class
需要被定义为
final
在JVM
中
,内部类不是直接调用方法的
参数
,而是内部类将传进来的
参数
通过自己的构造器备份到了自己的内部,自己内部的方法调用的实际是自己的属性而不是外部类方法的
参数
。 比如:
class
A{
final
string a="aabbcc";
class
B{ print(a); } } 类A
中
的内部类B的函数print()调用了定义在A
中
的
参数
a,这个...
java报错:Variable ‘i‘ is accessed from within
inner
class
, needs to be
final
or effectively
final
Variable ‘i’ is accessed from within
inner
class
, needs to be
final
or effectively
final
class
Sample{ public static void main(String[] args) {
final
CountDownLatch count = new CountDownLatch(100); //java的线程池
final
ExecutorServi
inner
class
用法
问题提出:
inner
class
中
使用
的
参数
为什么
必须
为
final
啊? interface A { void f(); }
class
B { public A m(
final
int a) { return new A() { in
Java SE
62,630
社区成员
307,264
社区内容
发帖
与我相关
我的任务
Java SE
Java 2 Standard Edition
复制链接
扫一扫
分享
社区描述
Java 2 Standard Edition
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章