开篇
小A :通过上篇文章,内部匿名类我明白了,可是我还有一个疑问,那就是内部匿名类引用外部变量为什么得加final呢?
MDove :好,那咱们今天就针对这个问题,好好的聊一聊。
内部匿名类引用外部变量为什么得加final
MDove :咱们改写一段简单的代码:
public class Main { public static void main(String[] args) { Main main = new Main(); main.fun(); } public void fun() { // 这里为什么赋值为null,因为避免String常量对效果的影响 final String nameInner = null; lisenter = new FunLisenter() { @Override public void fun() { System.out.println(nameInner); } }.fun(); } }
MDove :首先,咱们先对这几行代码,先提一个问题:为什么内部匿名类能够访问到nameInner?一个方法,就是一个栈帧。对于 局部变量 来说,方法结束,栈帧弹出,局部变量烟消云散。那么为什么内部匿名类可以访问?
小A :对啊,为什么?
MDove :你来给我捧哏的?我问你问题呢?
小A :(小声哔哔)…我不知道啊。
MDove :让我们直接看 反编译 的 class文件 :
class Main$1 implements FunLisenter { Main$1(Main var1, String var2) { this.this$0 = var1; this.val$nameInner = var2; } public void fun() { System.out.println(this.val$nameInner); } }
MDove :不用解释了吧?这个例子不光解释了内部匿名类为什么能够访问局部变量,还展示了持有外部引用的问题。局部变量nameInner,被我们的编译期在生成匿名内部类的时候以参数的形式赋值给了我们内部持有的外部变量了。因此我们调用fun()方法时,就直接使用this.val$nameInner。
小A :原来是这样…那为啥一定要加final呢?
MDove :其实这很好理解,首先问你一个问题。从java代码上来看局部变量nameInner和匿名内部类的nameInner是同一个对象么?
小A :那还用问么!当然是一个啦…
MDove :没错,从外部看,它的确是同一个。但是我们也反编译了字节码,发现这二者并非是同一个对象。咱们设想一下:如果我们不加final。在Java的这种设计下,一定会造成这种情况:我们在内部匿名类中重新赋值,但是局部变量并不会同步发生变化。因为按照这种设计,重新赋值,完全就是俩个变量!因此为了避免这种情况,索性加上了final。修改值不同步?连修改都不能修改,还需要什么同步!
其他语言的设计方案
小A :感觉是一个很别扭的设计?其他语言也是这样么?
MDove :你别说,其他语言还真不是这样。比如同为面向对象语言的C#:C#在编译过程中隐式的把变量包装在一个类里边。因此就可以避免修改不同步的问题。接下来我们用Java模拟一下这种方案:
public class Main { public static void main(String[] args) { Main main = new Main(); main.fun(); } public void fun() { final TempModel tempModel = new TempModel("Haha"); System.out.println(tempModel.name); new FunLisenter() { @Override public void fun() { System.out.println(tempModel.name); tempModel.name = "Hehe"; } }.fun(); System.out.println(tempModel.name); } } public class TempMain { private String name; public TempMain(String name) { this.name = name; } }
MDove :我们用简单的一个对象,包装了我们想使用的变量。这样就达到了,不用final的效果。
小A :TempModel也加final了呀?
MDove :加final那是因为Java语言的规定,你仔细想想,这是一个对象。加不加final会对内部的值造成影响么?这也就是C#实现局部变量的原理。
小A :好像还真是这么回事,看样子底层设计真的是一个很有艺术的学问。
You actually reported this really well!
write my dissertation hire dissertation writer
You actually mentioned that well.
new online casino games [url=https://bestonlinecasinoreal.us/]casino games online real money[/url] best online casino real money
Kudos. Good stuff.
essays buy [url=https://seoqmail.com/]pay to write my essay[/url]
Thank you, I like it!
hire someone to write my essay write my law essay uk
You actually reported this perfectly!
how to write an about me page for a blog write my paper for me
You definitely made your point!
essay writer free essay writer
Well expressed truly! .
essay writer reviews how to write an about me bio for blog
Thank you, Helpful stuff.
[url=https://writinganessaycollegeservice.com/]essay writing service reviews[/url] essay writing service 3 hours [url=https://essayservicehelp.com/]cheap custom essay writing service[/url] college essay writing help
You actually suggested this perfectly.
writing essay help essay writing service uk
Cheers. Loads of information.
professional essay writing services cheap essay writing service
Nicely put, Kudos!
cheap custom essay writing service cheap paper writing service
Kudos, Great stuff!
admission essay writing service reliable essay writing service
Nicely put. Many thanks.
coursework writers do my math homework
You actually mentioned that wonderfully.
hire a writer for an essay write a essay
This is nicely put. .
essay proposal proposal writers
Amazing content. Appreciate it!
pay to write a paper buy college paper online
Amazing material. With thanks!
essay writing service law medical school personal statement
You said it perfectly.!
best resume writing service 2020 buy essay online writing service
Thanks. Good information.
do my finance homework do my finance homework
Nicely put. Many thanks.
pay to do my homework my homework
Nicely put. Thank you!
essay writers paper writer
Regards! Numerous facts!
writing a persuasive essay best essay writers
Perfectly voiced of course! !
dissertation writing services writing help
Nicely put. Thanks a lot.
writing dissertation dissertation writer
Terrific information, Many thanks.
dissertation proposal phd dissertation
This is nicely put. .
essay on service writing a personal essay
Superb write ups. Appreciate it!
essay writing professional essay writers
You expressed this perfectly.
essay writer free trial write a essay
Tips effectively regarded.!
college essay writing help essay writing website
This is nicely said! .
paper writer services research paper writer services
Awesome information. With thanks.
biology essay writing service help with college essay writing
Whoa quite a lot of fantastic knowledge.
paper writer services online paper writing service
Nicely put, Thanks.
pay someone to do my homework homework help
Valuable tips. Thanks.
proquest dissertations dissertations online
Thank you! Terrific stuff!
pay for paper buy college essays
Cheers! A good amount of content!
medical school personal statement how to write a college essay
Very good tips. Kudos!
essay writer essay writter
You have made your position quite well.!
do my finance homework reddit do my homework
Thanks a lot! A lot of info.
write my paper for me writing an opinion essay
You actually reported this very well!
pay for essay reviews pay to write paper
Kudos! Quite a lot of posts!
dissertation abstracts international proquest dissertations
Regards, Lots of knowledge!
research proposal cheap research paper writing service
You have made the point.
write my essay for me free essay writer free trial
Beneficial tips. Regards!
write my term paper thesis proposal
Very well expressed certainly! !
pay for essay pay for essay reviews
You’ve made your point pretty clearly..
is essay writing service legal essay paper writing service
Good information. Many thanks.
pay for an essay buy college essays
Superb material. Cheers.
writing essays service cheap custom writing service
Kudos! Loads of data!
writing a narrative essay legit essay writing services
Amazing plenty of superb info.
help with my essay help me write my essay
You suggested this terrifically!
writing a personal essay writing a persuasive essay
Superb stuff. Thank you.
write my research paper for me thesis proposal
Whoa a good deal of valuable info.
essay writing practice online essay writing services
You actually explained it perfectly.
phd weight loss proquest dissertations
Seriously tons of very good material.
research proposal parts of a research proposal
Terrific tips. Thanks.
college essay writing service coursework writing service
Amazing loads of superb material.
persuasive essay writer write my paper for me free
Nicely put. Thank you.
best essay writers website that writes papers for you
You mentioned it superbly!
free writing assistant help writing essay
You actually explained it really well!
buy essay papers buy essay online
You suggested that perfectly.
phd dissertation writing dissertation
Well voiced genuinely. !
buy essays cheap pay for college papers
Cochrane Database of Systematic Reviews 2016, Issue 12
Fine data. Many thanks!
good essay writing service essay writing paper
You revealed this adequately!
cheap essay writing service us essay writing skills
You actually revealed that fantastically.
automatic essay writer paper writers
Incredible a lot of great material!
dissertation data analysis help dissertation abstracts
You actually suggested that really well.
dissertation writer dissertation proposal
write me an essay make an essay
how to write a expository essay
Tips clearly utilized!!
essay writing courses online free paper writing service college
Thank you! Awesome information!
paper writing service order custom paper
This is nicely put. !
paper writing services legitimate best online essay writing service
write my term paper parts of a research proposal
free writing help
Great stuff, With thanks.
writing essay services best college paper writing service
With thanks! Good stuff!
write my research paper for me essay writers
Incredible a lot of useful information.
thesis titles thesis paper
write my essay for cheap persuasive essay writer
writing 5 paragraph essay
With thanks! A lot of forum posts.
phd dissertation help dissertation abstract
Nicely put, Thank you.
online paper writing service essay writing service blog
With thanks. A good amount of postings!
pay for paper pay for essay papers
dissertation writing services reviews phd dissertation help
writing an explanatory essay
Seriously plenty of amazing knowledge.
pay for an essay essays for sale
Many thanks! I like it!
help with my essay college application essay help
urgent essay writing service essay writing sites
essays services
Thank you! Plenty of advice.
dissertation writers dissertation editing services
Regards. Loads of postings.
write me an essay write a essay for me
Nicely put. Thanks!
do my homework for free do my math homework for me
phd dissertation writing best dissertation
customize writing
You’ve made your point.
pay to write essay order essay online
Reliable data. Thank you.
buy a paper buy a paper for college
Really loads of terrific advice.
education essay writing service letter writing service
pay to write essay order essay online
writers wanted online
With thanks, Ample forum posts!
best essay writers paper writer services
Seriously all kinds of terrific knowledge!
writing essays service writing a good essay
Thanks a lot. Quite a lot of info!
help with essay writing argumentative essay
essay writer free trial essay writer
write my essay review
You said it perfectly.!
thesis proposal example thesis creator
Nicely put, Regards.
website that writes essays for you online essay writer
Thank you, Loads of tips!
college paper writing service what is the best essay writing service
thesis topic write a thesis
does money buy happiness essay
With thanks! I value it.
research proposal buy term papers online
Nicely put. Thanks.
college essay service essay writing service oxford
You made your point very well.!
resume writing services student essay writing
dissertation meaning custom dissertation writing services
uc college essay
You said it perfectly.!
pay someone to write my college essay buy essay online
Fine info. Kudos!
write my essay for cheap write my paper for me
Seriously lots of very good knowledge.
research paper writing service best paper writing services
dissertation editing services writing help
write my essay canada
Kudos, Lots of forum posts.
phd paper dissertation abstracts
You’ve made your point very well.!
help writing essay essays help
Really tons of excellent info!
do my college homework reddit do my homework
dissertation def proquest dissertations
copywriting service
Kudos, I appreciate this!
doctoral dissertation dissertation writers
Terrific posts, Many thanks!
thesis writing service thesis help
Terrific facts. Thanks!
dissertation data analysis help dissertation writers
professional essay writers write my paper
custom resume writing
With thanks, Ample data!
pay to do my homework coursework
Amazing postings. Cheers.
pay to write essay pay for essay online
Good forum posts, Appreciate it.
need help writing an essay essay writing service
good thesis statements thesis help
writing assignment help
Fine data. Regards!
pay someone to write paper pay for papers
Thank you, Awesome stuff.
phd paper best dissertation
Amazing knowledge. Appreciate it!
pay for essay reviews pay for essays
thesis paper write a thesis
buying essay
Truly a good deal of excellent knowledge.
pay someone to write my paper write paper for me