{"id":376,"date":"2019-07-10T14:52:21","date_gmt":"2019-07-10T06:52:21","guid":{"rendered":"https:\/\/103.249.111.182\/?p=376"},"modified":"2019-07-10T14:52:21","modified_gmt":"2019-07-10T06:52:21","slug":"java-local-variable-xx-defined-in-an-enclosing-scope-must-be-final-or-effectively-final","status":"publish","type":"post","link":"https:\/\/7gugu.com\/index.php\/2019\/07\/10\/java-local-variable-xx-defined-in-an-enclosing-scope-must-be-final-or-effectively-final\/","title":{"rendered":"[\u7b14\u8bb0]Java\u62a5\u9519Local variable XX defined in an enclosing scope &#8230;"},"content":{"rendered":"<p>\u4eca\u5929\u5199JAVA\u4ee3\u7801\u7684\u65f6\u5019,\u78b0\u89c1\u4e86\u4e00\u4e2a\u8fd9\u4e48\u4e2a\u62a5\u9519,\u7531\u4e8e\u89e3\u51b3\u65b9\u6cd5\u7b2c\u4e00\u6b21\u77e5\u6653,\u6545\u5728\u6b64\u7559\u4e0b\u7b14\u8bb0\u3002<\/p>\n<h1>\u539f\u4ee3\u7801:<\/h1>\n<pre class=\"Java\">for(int i = 0; i &lt; tabName.length; i++) {\n  Btn = new JButton(tabName[i]);\n  Btn.setPreferredSize(new Dimension(\n    100, 25));\n  Btn.addActionListener(new ActionListener() {\n      @Override \n      public void actionPerformed(ActionEvent e) {\n            tabObj.setSelectedIndex(i); \/\/BUG \n      } \n    }); \n  panel.add(Btn); \n}<\/pre>\n<h1>\u539f\u56e0:<\/h1>\n<p>\u5728BUG\u6807\u8bc6\u5904,\u6211\u7531\u4e8e\u4f7f\u7528\u4e86\u4e00\u4e2a\u533f\u540d\u7684\u672a\u77e5\u8303\u56f4\u7684\u53d8\u91cf,JAVA\u7f16\u8bd1\u5668\u5c31\u629b\u51fa\u4e86\u9519\u8bef&#8221;<span style=\"color: #ff0000;\"><strong>Local variable i defined in an enclosing scope must be final or effectively final<\/strong><\/span>&#8220;\u3002<\/p>\n<h1>\u89e3\u51b3\u65b9\u6848:<\/h1>\n<pre class=\"Java\">for(int i = 0; i &amp; amp; lt; tabName.length; i++) {\n  Btn = new JButton(tabName[i]);\n  Btn.setPreferredSize(new Dimension(100, 25));\n  final int j = i; \/\/\u901a\u8fc7\u8fd9\u4e2a\u8f6c\u6362,\u628a\u4e00\u4e2a\u65e0\u8303\u56f4\u7684\u533f\u540d\u53d8\u91cf\u8f6c\u6362\u6210final\u53d8\u91cf \/\/FIX \n  Btn.addActionListener(new ActionListener(){ \n      @Override \n      public void actionPerformed(ActionEvent e){ \n        tabObj.setSelectedIndex(j);\/\/FIX \n      } \n    }); \n  panel.add(Btn); \n}<\/pre>\n<p>\u901a\u8fc7\u4f7f\u7528<em><strong><span style=\"color: #ff0000;\">final int j<\/span><\/strong><\/em>\u8fd9\u4e2a\u4e2d\u95f4\u53d8\u91cf,java8\u7684\u7f16\u8bd1\u5668\u5c31\u4f1a\u8ba4\u4e3aj\u662f\u4e00\u4e2a\u5e38\u91cf,\u662f\u5b89\u5168\u53ef\u7528\u7684,\u56e0\u6b64\u5c31\u4e0d\u4f1a\u629b\u51fa\u4e0a\u8ff0\u9519\u8bef,\u6545\u89e3\u51b3\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4eca\u5929\u5199JAVA\u4ee3\u7801\u7684\u65f6\u5019,\u78b0\u89c1\u4e86\u4e00\u4e2a\u8fd9\u4e48\u4e2a\u62a5\u9519,\u7531\u4e8e\u89e3\u51b3\u65b9\u6cd5\u7b2c\u4e00\u6b21\u77e5\u6653,\u6545\u5728\u6b64\u7559\u4e0b\u7b14\u8bb0\u3002 \u539f\u4ee3\u7801: for(i &hellip; <a href=\"https:\/\/7gugu.com\/index.php\/2019\/07\/10\/java-local-variable-xx-defined-in-an-enclosing-scope-must-be-final-or-effectively-final\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u201c[\u7b14\u8bb0]Java\u62a5\u9519Local variable XX defined in an enclosing scope &#8230;\u201d<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,29,13],"tags":[],"class_list":["post-376","post","type-post","status-publish","format-standard","hentry","category-java","category-29","category-13"],"_links":{"self":[{"href":"https:\/\/7gugu.com\/index.php\/wp-json\/wp\/v2\/posts\/376","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/7gugu.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/7gugu.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/7gugu.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/7gugu.com\/index.php\/wp-json\/wp\/v2\/comments?post=376"}],"version-history":[{"count":0,"href":"https:\/\/7gugu.com\/index.php\/wp-json\/wp\/v2\/posts\/376\/revisions"}],"wp:attachment":[{"href":"https:\/\/7gugu.com\/index.php\/wp-json\/wp\/v2\/media?parent=376"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/7gugu.com\/index.php\/wp-json\/wp\/v2\/categories?post=376"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/7gugu.com\/index.php\/wp-json\/wp\/v2\/tags?post=376"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}