{"id":163330,"date":"2020-10-03T20:02:25","date_gmt":"2020-10-03T12:02:25","guid":{"rendered":"http:\/\/4563.org\/?p=163330"},"modified":"2020-10-03T20:02:25","modified_gmt":"2020-10-03T12:02:25","slug":"%e5%85%b3%e4%ba%8e-java-why-are-only-final-variables-accessible-in-anonymous-class-stack-overflow%e7%9a%84%e7%96%91%e6%83%91","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=163330","title":{"rendered":"\u5173\u4e8e\u201c Java &#8211; Why are only final variables accessible in anonymous class? &#8211; Stack Overflow\u201d\u7684\u7591\u60d1"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u5173\u4e8e\u201c Java &#8211; Why are only final variables accessible in anonymous class? &#8211; Stack Overflow\u201d\u7684\u7591\u60d1               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : JasonLaw <\/span>  <span><i><\/i> 0<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u5728java &#8211; Why are only final variables accessible in anonymous class? &#8211; Stack Overflow\u4e2d\uff0cJon Skeet \u8bf4\u4e86\u8fd9\u4e48\u4e00\u6bb5\u8bdd\uff1a<\/p>\n<blockquote>\n<p>When you create an instance of an anonymous inner class, any variables which are used within that class have their values copied in via the autogenerated constructor. This avoids the compiler having to autogenerate various extra types to hold the logical state of the &#8220;local variables&#8221;, as for example the C# compiler does&#8230; (When C# captures a variable in an anonymous function, it really captures the variable &#8211; the closure can update the variable in a way which is seen by the main body of the method, and vice versa.)<\/p>\n<\/blockquote>\n<p>\u6211\u5bf9\u201cThis avoids the compiler having to autogenerate various extra types to hold the logical state of the &#8220;local variables&#8221;, as for example the C# compiler does&#8230;\u201d\u7684\u7406\u89e3\u662f\uff1a\u5982\u679c\u8981\u5b9e\u73b0 full closures support\uff0c\u90a3\u4e48\u5c31\u9700\u8981\u4ea7\u751f\u4e00\u4e2a\u5171\u4eab\u7684\u6570\u636e\u7ed3\u6784\u7528\u6765\u5305\u542b\u5916\u90e8\u65b9\u6cd5\u548c\u5185\u90e8\u7c7b\u6240\u5171\u540c\u8bbf\u95ee\u7684\u53d8\u91cf\uff0c\u8fd9\u6837\u4e24\u8005\u90fd\u662f\u4f7f\u7528\u540c\u4e00\u4e2a\u6570\u636e\u6e90\uff0c\u90a3\u4e48\u4e00\u65b9\u5bf9\u53d8\u91cf\u7684\u6539\u53d8\u5c31\u4f1a\u5bf9\u53e6\u4e00\u65b9\u53ef\u89c1\uff0c\u7c7b\u4f3cIvan Dubrov \u6240\u8bf4\u7684\u3002<\/p>\n<p>\u6211\u4e0d\u80af\u5b9a\u6211\u7684\u7406\u89e3\u662f\u5426\u6b63\u786e\uff0c\u6240\u4ee5\u6211\u60f3\u770b\u4e00\u4e0b\u5927\u5bb6\u5bf9\u8fd9\u53e5\u8bdd\u7684\u7406\u89e3\u3002<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>5<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"3563710\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : mightofcode <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6211\u89c9\u5f97\u4f60\u7684\u7406\u89e3\u6ca1\u9519                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3563711\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : lxk11153 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u597d\u50cf\u6709\u70b9\u9053\u7406 [doge]                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3563712\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : javlib <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u90a3\u4e48\u4ee5\u540e\u4f1a\u4e0d\u4f1a\u6539\u6210\u975e final \u53d8\u91cf\u4e5f\u80fd\u5728\u95ed\u5305\u4f7f\u7528\u5462\uff1f\u5c31\u50cf\u73b0\u5728\u7684 c#                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3563713\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : JasonLaw <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @javlib \u4e0d\u77e5\u9053                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3563714\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : yazinnnn <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u95ed\u5305\u662f\u4ee3\u7801\u5757\u5f02\u5730\u6267\u884c\u7684\u624b\u6bb5\uff0c\u6bcf\u4e2a\u4ee3\u7801\u5757\u5b9e\u4f8b\u6240\u5305\u542b\u7684\u53d8\u91cf\uff08\u5305\u62ec\u5916\u90e8\u53d8\u91cf\uff09\u653e\u5728\u540c\u4e00\u4e2a\u4e0a\u4e0b\u6587\u91cc\u3002\u3002\u6bcf\u4e2a\u95ed\u5305\u8fc7\u7a0b\u5b9e\u4f8b\u6709\u72ec\u7acb\u7684\u751f\u547d\u5468\u671f<\/p>\n<p>\u5982\u679c\u4f60\u7684\u67d0\u4e2a\u4ee3\u7801\u5757\u6ca1\u6709\u56de\u6536\uff0c\u90a3\u8fd9\u4e2a\u4e0a\u4e0b\u6587\u91cc\u7684\u76f8\u5173\u53d8\u91cf\u4e5f\u4e0d\u4f1a\u56de\u6536                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u5173\u4e8e\u201c Java &#8211; &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/163330"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=163330"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/163330\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=163330"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=163330"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=163330"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}