{"id":203317,"date":"2025-05-22T09:00:51","date_gmt":"2025-05-22T01:00:51","guid":{"rendered":"https:\/\/server.hk\/cnblog\/203317\/"},"modified":"2025-05-22T09:00:51","modified_gmt":"2025-05-22T01:00:51","slug":"%e4%bd%bf%e7%94%a8%e9%9d%99%e6%80%81%e5%9d%97%e4%bc%98%e5%8c%96-java-%e4%bb%a3%e7%a0%81%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%8a%80%e6%9c%af","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/203317\/","title":{"rendered":"\u4f7f\u7528\u9759\u6001\u5757\u4f18\u5316 Java \u4ee3\u7801\u7684\u57fa\u672c\u6280\u672f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u4f7f\u7528\u9759\u6001\u5757\u4f18\u5316 Java \u4ee3\u7801\u7684\u57fa\u672c\u6280\u672f<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span>    <\/p>\n<p>\u4eca\u5929\u5c06\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u4f7f\u7528\u9759\u6001\u5757\u4f18\u5316 Java \u4ee3\u7801\u7684\u57fa\u672c\u6280\u672f\u300b<\/span>\uff0c\u611f\u5174\u8da3\u7684\u670b\u53cb\u8bf7\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427\uff01\u4ee5\u4e0b\u5185\u5bb9\u5c06\u4f1a\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u662f\u6b63\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u6216\u8005\u5df2\u7ecf\u662f\u5927\u4f6c\u7ea7\u522b\u4e86\uff0c\u90fd\u975e\u5e38\u6b22\u8fce\u4e5f\u5e0c\u671b\u5927\u5bb6\u90fd\u80fd\u7ed9\u6211\u5efa\u8bae\u8bc4\u8bba\u54c8~\u5e0c\u671b\u80fd\u5e2e\u52a9\u5230\u5927\u5bb6\uff01<\/p>\n<h2> 1. \u7406\u89e3\u9759\u6001\u5757 <\/h2>\n<p>\u9759\u6001\u5757\u662f java \u7c7b\u4e2d\u7684\u4ee3\u7801\u5757\uff0c\u5f53\u7c7b\u9996\u6b21\u52a0\u8f7d\u5230\u5185\u5b58\u4e2d\u65f6\u6267\u884c\u3002\u5b83\u4eec\u7528\u4e8e\u521d\u59cb\u5316\u9759\u6001\u53d8\u91cf\u5e76\u6267\u884c\u4ec5\u5e94\u5b8c\u6210\u4e00\u6b21\u7684\u8bbe\u7f6e\u64cd\u4f5c\u3002\u4e86\u89e3\u5982\u4f55\u4f7f\u7528\u9759\u6001\u5757\u53ef\u4ee5\u5e2e\u52a9\u60a8\u6709\u6548\u5730\u7ba1\u7406\u590d\u6742\u7684\u521d\u59cb\u5316\u8fc7\u7a0b\u3002 <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241104\/17307209206728b4980c085.jpg\" class=\"aligncenter\"><\/p>\n<h3> 1.1 \u4ec0\u4e48\u662f\u9759\u6001\u5757\uff1f <\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241104\/17307209246728b49c71076.png\" style=\"max-width:100%\" class=\"aligncenter\"><\/p>\n<p>\u9759\u6001\u5757\u662f\u4f7f\u7528 static \u5173\u952e\u5b57\u5b9a\u4e49\u7684\uff0c\u540e\u8ddf\u4ee3\u7801\u5757\u3002\u5b83\u5728\u7c7b\u9996\u6b21\u52a0\u8f7d\u65f6\u8fd0\u884c\u4e00\u6b21\uff0c\u5bf9\u4e8e\u521d\u59cb\u5316\u9759\u6001\u53d8\u91cf\u6216\u6267\u884c\u8bbe\u7f6e\u4efb\u52a1\u5f88\u6709\u7528\u3002 <\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>public class config {\n    static {\n        system.out.println(\"static block executed\");\n        \/\/ initialize static resources here\n    }\n\n    public static void main(string[] args) {\n        system.out.println(\"main method executed\");\n    }\n}\n<\/pre>\n<p><strong>\u6f14\u793a\u4ee3\u7801\u548c\u7ed3\u679c<\/strong>\uff1a\u8fd0\u884c\u4e0a\u8ff0\u4ee3\u7801\u5c06\u4ea7\u751f\u4ee5\u4e0b\u8f93\u51fa\uff1a<\/p>\n<pre>static block executed\nmain method executed\n<\/pre>\n<p>\u9759\u6001\u5757\u5728main\u65b9\u6cd5\u4e4b\u524d\u6267\u884c\uff0c\u5c55\u793a\u5176\u521d\u59cb\u5316\u4f5c\u7528\u3002<\/p>\n<h3> 1.2 \u9759\u6001\u5757\u7684\u7528\u4f8b <\/h3>\n<p>\u9759\u6001\u5757\u901a\u5e38\u7528\u4e8e\uff1a<\/p>\n<ul>\n<li>\u52a0\u8f7d\u914d\u7f6e\u6587\u4ef6\u3002<\/li>\n<li>\u7528\u590d\u6742\u7684\u903b\u8f91\u521d\u59cb\u5316\u9759\u6001\u53d8\u91cf\u3002<\/li>\n<li>\u6267\u884c\u53ea\u5e94\u53d1\u751f\u4e00\u6b21\u7684\u8d44\u6e90\u5206\u914d\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>public class databaseconfig {\n    private static connection connection;\n\n    static {\n        try {\n            \/\/ simulate loading database configuration\n            system.out.println(\"loading database configuration...\");\n            connection = drivermanager.getconnection(\"jdbc:mysql:\/\/localhost:3306\/mydb\", \"user\", \"password\");\n        } catch (sqlexception e) {\n            e.printstacktrace();\n        }\n    }\n\n    public static connection getconnection() {\n        return connection;\n    }\n}\n<\/pre>\n<p><strong>\u6f14\u793a\u4ee3\u7801\u548c\u7ed3\u679c<\/strong> \uff1a\u9996\u6b21\u52a0\u8f7d <strong>databaseconfig<\/strong> \u7c7b\u65f6\uff0c\u9759\u6001\u5757\u4f1a\u521d\u59cb\u5316\u6570\u636e\u5e93\u8fde\u63a5\u3002\u8fd9\u53ef\u786e\u4fdd\u8fde\u63a5\u8bbe\u7f6e\u5904\u7406\u4e00\u6b21\u5e76\u5728\u6574\u4e2a\u5e94\u7528\u7a0b\u5e8f\u4e2d\u91cd\u590d\u4f7f\u7528\u3002 <\/p>\n<h2> 2. \u4f7f\u7528\u9759\u6001\u5757\u7684\u6700\u4f73\u5b9e\u8df5 <\/h2>\n<p>\u4f7f\u7528\u9759\u6001\u5757\u65f6\uff0c\u9075\u5faa\u6700\u4f73\u5b9e\u8df5\u6709\u52a9\u4e8e\u786e\u4fdd\u4ee3\u7801\u9ad8\u6548\u4e14\u65e0\u9519\u8bef\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u9700\u8981\u8bb0\u4f4f\u7684\u5173\u952e\u505a\u6cd5\uff1a<\/p>\n<h3> 2.1 \u907f\u514d\u7e41\u91cd\u7684\u8ba1\u7b97 <\/h3>\n<p>\u9759\u6001\u5757\u4e0d\u5e94\u5305\u542b\u7e41\u91cd\u7684\u8ba1\u7b97\u6216\u5197\u957f\u7684\u64cd\u4f5c\u3002\u5b83\u4eec\u4f1a\u51cf\u6162\u7c7b\u52a0\u8f7d\u8fc7\u7a0b\u5e76\u5f71\u54cd\u5e94\u7528\u7a0b\u5e8f\u542f\u52a8\u65f6\u95f4\u3002<\/p>\n<pre>public class heavyinitialization {\n    static {\n        \/\/ avoid heavy computations here\n        system.out.println(\"heavy computation should be avoided in static blocks.\");\n    }\n}\n<\/pre>\n<p>\u786e\u4fdd\u9759\u6001\u5757\u7528\u4e8e\u8f7b\u578b\u521d\u59cb\u5316\u4efb\u52a1\u3002\u7e41\u91cd\u7684\u64cd\u4f5c\u5e94\u63a8\u8fdf\u5230\u5176\u4ed6\u65b9\u6cd5\u6216\u5f02\u6b65\u5904\u7406\u3002 <\/p>\n<h3> 2.2 \u6b63\u786e\u5904\u7406\u5f02\u5e38 <\/h3>\n<p>\u9759\u6001\u5757\u5728\u521d\u59cb\u5316\u671f\u95f4\u53ef\u80fd\u4f1a\u629b\u51fa\u5f02\u5e38\u3002\u5fc5\u987b\u6b63\u786e\u5904\u7406\u8fd9\u4e9b\u5f02\u5e38\uff0c\u4ee5\u907f\u514d\u7c7b\u52a0\u8f7d\u671f\u95f4\u51fa\u73b0\u95ee\u9898\u3002<\/p>\n<pre>public class safeconfig {\n    static {\n        try {\n            \/\/ example of safe exception handling\n            system.out.println(\"initializing resources...\");\n            throw new ioexception(\"failed to initialize resources\");\n        } catch (ioexception e) {\n            system.err.println(\"exception in static block: \" + e.getmessage());\n        }\n    }\n}\n<\/pre>\n<p>\u5904\u7406\u9759\u6001\u5757\u4e2d\u7684\u5f02\u5e38\u53ef\u786e\u4fdd\u7c7b\u52a0\u8f7d\u4e0d\u4f1a\u7a81\u7136\u5931\u8d25\u3002\u5e94\u5b9e\u65bd\u9002\u5f53\u7684\u9519\u8bef\u6d88\u606f\u548c\u5904\u7406\u7b56\u7565\u3002 <\/p>\n<h3> 2.3 \u6700\u5c0f\u5316\u4f9d\u8d56\u6027 <\/h3>\n<p>\u6700\u5c0f\u5316\u9759\u6001\u5757\u4e2d\u7684\u4f9d\u8d56\u6027\u548c\u526f\u4f5c\u7528\u3002\u4ed6\u4eec\u5e94\u8be5\u4e3b\u8981\u5173\u6ce8\u521d\u59cb\u5316\u4efb\u52a1\uff0c\u800c\u4e0d\u662f\u4f9d\u8d56\u5916\u90e8\u72b6\u6001\u6216\u6267\u884c\u53ef\u80fd\u5931\u8d25\u7684\u64cd\u4f5c\u3002<\/p>\n<pre>public class MinimalDependency {\n    static {\n        \/\/ Minimize dependencies in static block\n        System.out.println(\"Static block with minimal dependencies.\");\n    }\n}\n<\/pre>\n<p>\u5177\u6709\u6700\u5c0f\u4f9d\u8d56\u6027\u7684\u9759\u6001\u5757\u53ef\u786e\u4fdd\u7c7b\u52a0\u8f7d\u4fdd\u6301\u9ad8\u6548\u5e76\u4e14\u4e0d\u6613\u56e0\u5916\u90e8\u56e0\u7d20\u800c\u51fa\u9519\u3002 <\/p>\n<h2> \u4e09\u3001\u7ed3\u8bba <\/h2>\n<p>\u9759\u6001\u5757\u662f\u5728 java \u4e2d\u521d\u59cb\u5316\u9759\u6001\u53d8\u91cf\u548c\u6267\u884c\u8bbe\u7f6e\u4efb\u52a1\u7684\u5f3a\u5927\u5de5\u5177\u3002\u901a\u8fc7\u9075\u5faa\u8fd9\u4e9b\u6700\u4f73\u5b9e\u8df5\uff08\u4e86\u89e3\u5b83\u4eec\u7684\u4f5c\u7528\u3001\u907f\u514d\u7e41\u91cd\u7684\u8ba1\u7b97\u3001\u6b63\u786e\u5904\u7406\u5f02\u5e38\u4ee5\u53ca\u6700\u5927\u9650\u5ea6\u5730\u51cf\u5c11\u4f9d\u8d56\u6027\uff09\uff0c\u60a8\u53ef\u4ee5\u4f18\u5316 java \u4ee3\u7801\u4ee5\u83b7\u5f97\u66f4\u597d\u7684\u6027\u80fd\u548c\u53ef\u9760\u6027\u3002 <\/p>\n<p>\u5bf9\u9759\u6001\u5757\u6709\u4efb\u4f55\u7591\u95ee\u6216\u9700\u8981\u8fdb\u4e00\u6b65\u8bf4\u660e\u5417\uff1f\u6b22\u8fce\u5728\u4e0b\u9762\u8bc4\u8bba\uff01 <\/p>\n<p><strong>\u9605\u8bfb\u66f4\u591a\u5e16\u5b50<\/strong>\uff1a\u4f7f\u7528\u9759\u6001\u5757\u4f18\u5316 java \u4ee3\u7801\u7684\u57fa\u672c\u6280\u672f<\/p>\n<p>\u4eca\u5929\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff1b\u5173\u4e8e\u6587\u7ae0\u7684\u6280\u672f\u77e5\u8bc6\u6211\u4eec\u4f1a\u4e00\u70b9\u70b9\u6df1\u5165\u4ecb\u7ecd\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u4e00\u8d77\u5b66\u4e60\u7f16\u7a0b~<\/p>\n<p>      \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1adev.to \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb\u5220\u9664<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528\u9759\u6001\u5757\u4f18\u5316 Java \u4ee3\u7801\u7684&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-203317","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/203317","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=203317"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/203317\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=203317"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=203317"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=203317"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}