java static作用 java中static作用是什么?

java\u4e2dstatic\u4f5c\u7528\u8be6\u89e3

\u6709\u8981\u8be6\u89e3,\u90a3\u6211\u627e\u8d44\u6599\u7ed9\u4f60\u5427

static\u8868\u793a\u201c\u5168\u5c40\u201d\u6216\u8005\u201c\u9759\u6001\u201d\u7684\u610f\u601d\uff0c\u7528\u6765\u4fee\u9970\u6210\u5458\u53d8\u91cf\u548c\u6210\u5458\u65b9\u6cd5\uff0c\u4e5f\u53ef\u4ee5\u5f62\u6210\u9759\u6001static\u4ee3\u7801\u5757\uff0c\u4f46\u662fJava\u8bed\u8a00\u4e2d\u6ca1\u6709\u5168\u5c40\u53d8\u91cf\u7684\u6982\u5ff5\u3002

\u88abstatic\u4fee\u9970\u7684\u6210\u5458\u53d8\u91cf\u548c\u6210\u5458\u65b9\u6cd5\u72ec\u7acb\u4e8e\u8be5\u7c7b\u7684\u4efb\u4f55\u5bf9\u8c61\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u5b83\u4e0d\u4f9d\u8d56\u7c7b\u7279\u5b9a\u7684\u5b9e\u4f8b\uff0c\u88ab\u7c7b\u7684\u6240\u6709\u5b9e\u4f8b\u5171\u4eab\u3002

\u53ea\u8981\u8fd9\u4e2a\u7c7b\u88ab\u52a0\u8f7d\uff0cJava\u865a\u62df\u673a\u5c31\u80fd\u6839\u636e\u7c7b\u540d\u5728\u8fd0\u884c\u65f6\u6570\u636e\u533a\u7684\u65b9\u6cd5\u533a\u5185\u5b9a\u627e\u5230\u4ed6\u4eec\u3002\u56e0\u6b64\uff0cstatic\u5bf9\u8c61\u53ef\u4ee5\u5728\u5b83\u7684\u4efb\u4f55\u5bf9\u8c61\u521b\u5efa\u4e4b\u524d\u8bbf\u95ee\uff0c\u65e0\u9700\u5f15\u7528\u4efb\u4f55\u5bf9\u8c61\u3002

\u7528public\u4fee\u9970\u7684static\u6210\u5458\u53d8\u91cf\u548c\u6210\u5458\u65b9\u6cd5\u672c\u8d28\u662f\u5168\u5c40\u53d8\u91cf\u548c\u5168\u5c40\u65b9\u6cd5\uff0c\u5f53\u58f0\u660e\u5b83\u7c7b\u7684\u5bf9\u8c61\u5e02\uff0c\u4e0d\u751f\u6210static\u53d8\u91cf\u7684\u526f\u672c\uff0c\u800c\u662f\u7c7b\u7684\u6240\u6709\u5b9e\u4f8b\u5171\u4eab\u540c\u4e00\u4e2astatic\u53d8\u91cf\u3002

static\u53d8\u91cf\u524d\u53ef\u4ee5\u6709private\u4fee\u9970\uff0c\u8868\u793a\u8fd9\u4e2a\u53d8\u91cf\u53ef\u4ee5\u5728\u7c7b\u7684\u9759\u6001\u4ee3\u7801\u5757\u4e2d\uff0c\u6216\u8005\u7c7b\u7684\u5176\u4ed6\u9759\u6001\u6210\u5458\u65b9\u6cd5\u4e2d\u4f7f\u7528\uff08\u5f53\u7136\u4e5f\u53ef\u4ee5\u5728\u975e\u9759\u6001\u6210\u5458\u65b9\u6cd5\u4e2d\u4f7f\u7528--\u5e9f\u8bdd\uff09\uff0c\u4f46\u662f\u4e0d\u80fd\u5728\u5176\u4ed6\u7c7b\u4e2d\u901a\u8fc7\u7c7b\u540d\u6765\u76f4\u63a5\u5f15\u7528\uff0c\u8fd9\u4e00\u70b9\u5f88\u91cd\u8981\u3002\u5b9e\u9645\u4e0a\u4f60\u9700\u8981\u641e\u660e\u767d\uff0cprivate\u662f\u8bbf\u95ee\u6743\u9650\u9650\u5b9a\uff0cstatic\u8868\u793a\u4e0d\u8981\u5b9e\u4f8b\u5316\u5c31\u53ef\u4ee5\u4f7f\u7528\uff0c\u8fd9\u6837\u5c31\u5bb9\u6613\u7406\u89e3\u591a\u4e86\u3002static\u524d\u9762\u52a0\u4e0a\u5176\u5b83\u8bbf\u95ee\u6743\u9650\u5173\u952e\u5b57\u7684\u6548\u679c\u4e5f\u4ee5\u6b64\u7c7b\u63a8\u3002

static\u4fee\u9970\u7684\u6210\u5458\u53d8\u91cf\u548c\u6210\u5458\u65b9\u6cd5\u4e60\u60ef\u4e0a\u79f0\u4e3a\u9759\u6001\u53d8\u91cf\u548c\u9759\u6001\u65b9\u6cd5\uff0c\u53ef\u4ee5\u76f4\u63a5\u901a\u8fc7\u7c7b\u540d\u6765\u8bbf\u95ee\uff0c\u8bbf\u95ee\u8bed\u6cd5\u4e3a\uff1a
\u7c7b\u540d.\u9759\u6001\u65b9\u6cd5\u540d(\u53c2\u6570\u5217\u8868...)
\u7c7b\u540d.\u9759\u6001\u53d8\u91cf\u540d

\u7528static\u4fee\u9970\u7684\u4ee3\u7801\u5757\u8868\u793a\u9759\u6001\u4ee3\u7801\u5757\uff0c\u5f53Java\u865a\u62df\u673a\uff08JVM\uff09\u52a0\u8f7d\u7c7b\u65f6\uff0c\u5c31\u4f1a\u6267\u884c\u8be5\u4ee3\u7801\u5757\uff08\u7528\u5904\u975e\u5e38\u5927\uff0c\u5475\u5475\uff09\u3002

1\u3001static\u53d8\u91cf
\u3000\u6309\u7167\u662f\u5426\u9759\u6001\u7684\u5bf9\u7c7b\u6210\u5458\u53d8\u91cf\u8fdb\u884c\u5206\u7c7b\u53ef\u5206\u4e24\u79cd\uff1a\u4e00\u79cd\u662f\u88abstatic\u4fee\u9970\u7684\u53d8\u91cf\uff0c\u53eb\u9759\u6001\u53d8\u91cf\u6216\u7c7b\u53d8\u91cf\uff1b\u53e6\u4e00\u79cd\u662f\u6ca1\u6709\u88abstatic\u4fee\u9970\u7684\u53d8\u91cf\uff0c\u53eb\u5b9e\u4f8b\u53d8\u91cf\u3002

\u4e24\u8005\u7684\u533a\u522b\u662f\uff1a
\u3000\u5bf9\u4e8e\u9759\u6001\u53d8\u91cf\u5728\u5185\u5b58\u4e2d\u53ea\u6709\u4e00\u4e2a\u62f7\u8d1d\uff08\u8282\u7701\u5185\u5b58\uff09\uff0cJVM\u53ea\u4e3a\u9759\u6001\u5206\u914d\u4e00\u6b21\u5185\u5b58\uff0c\u5728\u52a0\u8f7d\u7c7b\u7684\u8fc7\u7a0b\u4e2d\u5b8c\u6210\u9759\u6001\u53d8\u91cf\u7684\u5185\u5b58\u5206\u914d\uff0c\u53ef\u7528\u7c7b\u540d\u76f4\u63a5\u8bbf\u95ee\uff08\u65b9\u4fbf\uff09\uff0c\u5f53\u7136\u4e5f\u53ef\u4ee5\u901a\u8fc7\u5bf9\u8c61\u6765\u8bbf\u95ee\uff08\u4f46\u662f\u8fd9\u662f\u4e0d\u63a8\u8350\u7684\uff09\u3002
\u3000\u5bf9\u4e8e\u5b9e\u4f8b\u53d8\u91cf\uff0c\u6ca1\u521b\u5efa\u4e00\u4e2a\u5b9e\u4f8b\uff0c\u5c31\u4f1a\u4e3a\u5b9e\u4f8b\u53d8\u91cf\u5206\u914d\u4e00\u6b21\u5185\u5b58\uff0c\u5b9e\u4f8b\u53d8\u91cf\u53ef\u4ee5\u5728\u5185\u5b58\u4e2d\u6709\u591a\u4e2a\u62f7\u8d1d\uff0c\u4e92\u4e0d\u5f71\u54cd\uff08\u7075\u6d3b\uff09\u3002

\u6240\u4ee5\u4e00\u822c\u5728\u9700\u8981\u5b9e\u73b0\u4ee5\u4e0b\u4e24\u4e2a\u529f\u80fd\u65f6\u4f7f\u7528\u9759\u6001\u53d8\u91cf\uff1a
 \u5728\u5bf9\u8c61\u4e4b\u95f4\u5171\u4eab\u503c\u65f6
 \u65b9\u4fbf\u8bbf\u95ee\u53d8\u91cf\u65f6


2\u3001\u9759\u6001\u65b9\u6cd5
\u9759\u6001\u65b9\u6cd5\u53ef\u4ee5\u76f4\u63a5\u901a\u8fc7\u7c7b\u540d\u8c03\u7528\uff0c\u4efb\u4f55\u7684\u5b9e\u4f8b\u4e5f\u90fd\u53ef\u4ee5\u8c03\u7528\uff0c
\u56e0\u6b64\u9759\u6001\u65b9\u6cd5\u4e2d\u4e0d\u80fd\u7528this\u548csuper\u5173\u952e\u5b57\uff0c\u4e0d\u80fd\u76f4\u63a5\u8bbf\u95ee\u6240\u5c5e\u7c7b\u7684\u5b9e\u4f8b\u53d8\u91cf\u548c\u5b9e\u4f8b\u65b9\u6cd5(\u5c31\u662f\u4e0d\u5e26static\u7684\u6210\u5458\u53d8\u91cf\u548c\u6210\u5458\u6210\u5458\u65b9\u6cd5)\uff0c\u53ea\u80fd\u8bbf\u95ee\u6240\u5c5e\u7c7b\u7684\u9759\u6001\u6210\u5458\u53d8\u91cf\u548c\u6210\u5458\u65b9\u6cd5\u3002
\u56e0\u4e3a\u5b9e\u4f8b\u6210\u5458\u4e0e\u7279\u5b9a\u7684\u5bf9\u8c61\u5173\u8054\uff01\u8fd9\u4e2a\u9700\u8981\u53bb\u7406\u89e3\uff0c\u60f3\u660e\u767d\u5176\u4e2d\u7684\u9053\u7406\uff0c\u4e0d\u662f\u8bb0\u5fc6\uff01\uff01\uff01
\u56e0\u4e3astatic\u65b9\u6cd5\u72ec\u7acb\u4e8e\u4efb\u4f55\u5b9e\u4f8b\uff0c\u56e0\u6b64static\u65b9\u6cd5\u5fc5\u987b\u88ab\u5b9e\u73b0\uff0c\u800c\u4e0d\u80fd\u662f\u62bd\u8c61\u7684abstract\u3002

\u4f8b\u5982\u4e3a\u4e86\u65b9\u4fbf\u65b9\u6cd5\u7684\u8c03\u7528\uff0cJava API\u4e2d\u7684Math\u7c7b\u4e2d\u6240\u6709\u7684\u65b9\u6cd5\u90fd\u662f\u9759\u6001\u7684\uff0c\u800c\u4e00\u822c\u7c7b\u5185\u90e8\u7684static\u65b9\u6cd5\u4e5f\u662f\u65b9\u4fbf\u5176\u5b83\u7c7b\u5bf9\u8be5\u65b9\u6cd5\u7684\u8c03\u7528\u3002

\u9759\u6001\u65b9\u6cd5\u662f\u7c7b\u5185\u90e8\u7684\u4e00\u7c7b\u7279\u6b8a\u65b9\u6cd5\uff0c\u53ea\u6709\u5728\u9700\u8981\u65f6\u624d\u5c06\u5bf9\u5e94\u7684\u65b9\u6cd5\u58f0\u660e\u6210\u9759\u6001\u7684\uff0c\u4e00\u4e2a\u7c7b\u5185\u90e8\u7684\u65b9\u6cd5\u4e00\u822c\u90fd\u662f\u975e\u9759\u6001\u7684

3\u3001static\u4ee3\u7801\u5757

\u3000static\u4ee3\u7801\u5757\u4e5f\u53eb\u9759\u6001\u4ee3\u7801\u5757\uff0c\u662f\u5728\u7c7b\u4e2d\u72ec\u7acb\u4e8e\u7c7b\u6210\u5458\u7684static\u8bed\u53e5\u5757\uff0c\u53ef\u4ee5\u6709\u591a\u4e2a\uff0c\u4f4d\u7f6e\u53ef\u4ee5\u968f\u4fbf\u653e\uff0c\u5b83\u4e0d\u5728\u4efb\u4f55\u7684\u65b9\u6cd5\u4f53\u5185\uff0cJVM\u52a0\u8f7d\u7c7b\u65f6\u4f1a\u6267\u884c\u8fd9\u4e9b\u9759\u6001\u7684\u4ee3\u7801\u5757\uff0c\u5982\u679cstatic\u4ee3\u7801\u5757\u6709\u591a\u4e2a\uff0cJVM\u5c06\u6309\u7167\u5b83\u4eec\u5728\u7c7b\u4e2d\u51fa\u73b0\u7684\u5148\u540e\u987a\u5e8f\u4f9d\u6b21\u6267\u884c\u5b83\u4eec\uff0c\u6bcf\u4e2a\u4ee3\u7801\u5757\u53ea\u4f1a\u88ab\u6267\u884c\u4e00\u6b21\u3002\u4f8b\u5982\uff1a

public class Test5 {
private static int a;
private int b;

static{
Test5.a=3;
System.out.println(a);
Test5 t=new Test5();
t.f();
t.b=1000;
System.out.println(t.b);
}
static{
Test5.a=4;
System.out.println(a);
}
public static void main(String[] args) {
// TODO \u81ea\u52a8\u751f\u6210\u65b9\u6cd5\u5b58\u6839
}
static{
Test5.a=5;
System.out.println(a);
}
public void f(){
System.out.println("hhahhahah");
}
}

\u8fd0\u884c\u7ed3\u679c\uff1a
3
hhahhahah
1000
4
5

\u3000\u5229\u7528\u9759\u6001\u4ee3\u7801\u5757\u53ef\u4ee5\u5bf9\u4e00\u4e9bstatic\u53d8\u91cf\u8fdb\u884c\u8d4b\u503c\uff0c\u6700\u540e\u518d\u770b\u4e00\u773c\u8fd9\u4e9b\u4f8b\u5b50\uff0c\u90fd\u4e00\u4e2astatic\u7684main\u65b9\u6cd5\uff0c\u8fd9\u6837JVM\u5728\u8fd0\u884cmain\u65b9\u6cd5\u7684\u65f6\u5019\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528\u800c\u4e0d\u7528\u521b\u5efa\u5b9e\u4f8b\u3002

4\u3001static\u548cfinal\u4e00\u5757\u7528\u8868\u793a\u4ec0\u4e48
static final\u7528\u6765\u4fee\u9970\u6210\u5458\u53d8\u91cf\u548c\u6210\u5458\u65b9\u6cd5\uff0c\u53ef\u7b80\u5355\u7406\u89e3\u4e3a\u201c\u5168\u5c40\u5e38\u91cf\u201d\uff01
\u5bf9\u4e8e\u53d8\u91cf\uff0c\u8868\u793a\u4e00\u65e6\u7ed9\u503c\u5c31\u4e0d\u53ef\u4fee\u6539\uff0c\u5e76\u4e14\u901a\u8fc7\u7c7b\u540d\u53ef\u4ee5\u8bbf\u95ee\u3002
\u5bf9\u4e8e\u65b9\u6cd5\uff0c\u8868\u793a\u4e0d\u53ef\u8986\u76d6\uff0c\u5e76\u4e14\u53ef\u4ee5\u901a\u8fc7\u7c7b\u540d\u76f4\u63a5\u8bbf\u95ee\u3002

\u6709\u65f6\u4f60\u5e0c\u671b\u5b9a\u4e49\u4e00\u4e2a\u7c7b\u6210\u5458\uff0c\u4f7f\u5b83\u7684\u4f7f\u7528\u5b8c\u5168\u72ec\u7acb\u4e8e\u8be5\u7c7b\u7684\u4efb\u4f55\u5bf9\u8c61\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u7c7b\u6210\u5458\u5fc5\u987b\u901a\u8fc7\u5b83\u7684\u7c7b\u7684\u5bf9\u8c61\u8bbf\u95ee\uff0c\u4f46\u662f\u53ef\u4ee5\u521b\u5efa\u8fd9\u6837\u4e00\u4e2a\u6210\u5458\uff0c\u5b83\u80fd\u591f\u88ab\u5b83\u81ea\u5df1\u4f7f\u7528\uff0c\u800c\u4e0d\u5fc5\u5f15\u7528\u7279\u5b9a\u7684\u5b9e\u4f8b\u3002\u5728\u6210\u5458\u7684\u58f0\u660e\u524d\u9762\u52a0\u4e0a\u5173\u952e\u5b57static(\u9759\u6001\u7684)\u5c31\u80fd\u521b\u5efa\u8fd9\u6837\u7684\u6210\u5458\u3002\u5982\u679c\u4e00\u4e2a\u6210\u5458\u88ab\u58f0\u660e\u4e3astatic\uff0c\u5b83\u5c31\u80fd\u591f\u5728\u5b83\u7684\u7c7b\u7684\u4efb\u4f55\u5bf9\u8c61\u521b\u5efa\u4e4b\u524d\u88ab\u8bbf\u95ee\uff0c\u800c\u4e0d\u5fc5\u5f15\u7528\u4efb\u4f55\u5bf9\u8c61\u3002\u4f60\u53ef\u4ee5\u5c06\u65b9\u6cd5\u548c\u53d8\u91cf\u90fd\u58f0\u660e\u4e3astatic\u3002static \u6210\u5458\u7684\u6700\u5e38\u89c1\u7684\u4f8b\u5b50\u662fmain( ) \u3002\u56e0\u4e3a\u5728\u7a0b\u5e8f\u5f00\u59cb\u6267\u884c\u65f6\u5fc5\u987b\u8c03\u7528main() \uff0c\u6240\u4ee5\u5b83\u88ab\u58f0\u660e\u4e3astatic\u3002

\u58f0\u660e\u4e3astatic\u7684\u53d8\u91cf\u5b9e\u8d28\u4e0a\u5c31\u662f\u5168\u5c40\u53d8\u91cf\u3002\u5f53\u58f0\u660e\u4e00\u4e2a\u5bf9\u8c61\u65f6\uff0c\u5e76\u4e0d\u4ea7\u751fstatic\u53d8\u91cf\u7684\u62f7\u8d1d\uff0c\u800c\u662f\u8be5\u7c7b\u6240\u6709\u7684\u5b9e\u4f8b\u53d8\u91cf\u5171\u7528\u540c\u4e00\u4e2astatic\u53d8\u91cf\u3002\u58f0\u660e\u4e3astatic\u7684\u65b9\u6cd5\u6709\u4ee5\u4e0b\u51e0\u6761\u9650\u5236\uff1a

\u5b83\u4eec\u4ec5\u80fd\u8c03\u7528\u5176\u4ed6\u7684static \u65b9\u6cd5\u3002

\u5b83\u4eec\u53ea\u80fd\u8bbf\u95eestatic\u6570\u636e\u3002

\u5b83\u4eec\u4e0d\u80fd\u4ee5\u4efb\u4f55\u65b9\u5f0f\u5f15\u7528this \u6216super\uff08\u5173\u952e\u5b57super \u4e0e\u7ee7\u627f\u6709\u5173\uff0c\u5728\u4e0b\u4e00\u7ae0\u4e2d\u63cf\u8ff0\uff09\u3002
\u5982\u679c\u4f60\u9700\u8981\u901a\u8fc7\u8ba1\u7b97\u6765\u521d\u59cb\u5316\u4f60\u7684static\u53d8\u91cf\uff0c\u4f60\u53ef\u4ee5\u58f0\u660e\u4e00\u4e2astatic\u5757\uff0cStatic \u5757\u4ec5\u5728\u8be5\u7c7b\u88ab\u52a0\u8f7d\u65f6\u6267\u884c\u4e00\u6b21\u3002\u4e0b\u9762\u7684\u4f8b\u5b50\u663e\u793a\u7684\u7c7b\u6709\u4e00\u4e2astatic\u65b9\u6cd5\uff0c\u4e00\u4e9bstatic\u53d8\u91cf\uff0c\u4ee5\u53ca\u4e00\u4e2astatic \u521d\u59cb\u5316\u5757\uff1a
// Demonstrate static variables\uff0cmethods\uff0cand blocks.

class UseStatic {
static int a = 3;
static int b;

static void meth(int x) {
System.out.println("x = " + x);
System.out.println("a = " + a);
System.out.println("b = " + b);
}

static {
System.out.println("Static block initialized.");
b = a * 4;
}

public static void main(String args[]) {
meth(42);
}
}

\u4e00\u65e6UseStatic \u7c7b\u88ab\u88c5\u8f7d\uff0c\u6240\u6709\u7684static\u8bed\u53e5\u88ab\u8fd0\u884c\u3002\u9996\u5148\uff0ca\u88ab\u8bbe\u7f6e\u4e3a3\uff0c\u63a5\u7740static \u5757\u6267\u884c(\u6253\u5370\u4e00\u6761\u6d88\u606f)\uff0c\u6700\u540e\uff0cb\u88ab\u521d\u59cb\u5316\u4e3aa*4 \u621612\u3002\u7136\u540e\u8c03\u7528main()\uff0cmain() \u8c03\u7528meth() \uff0c\u628a\u503c42\u4f20\u9012\u7ed9x\u30023\u4e2aprintln ( ) \u8bed\u53e5\u5f15\u7528\u4e24\u4e2astatic\u53d8\u91cfa\u548cb\uff0c\u4ee5\u53ca\u5c40\u90e8\u53d8\u91cfx \u3002

\u6ce8\u610f\uff1a\u5728\u4e00\u4e2astatic \u65b9\u6cd5\u4e2d\u5f15\u7528\u4efb\u4f55\u5b9e\u4f8b\u53d8\u91cf\u90fd\u662f\u975e\u6cd5\u7684\u3002

\u4e0b\u9762\u662f\u8be5\u7a0b\u5e8f\u7684\u8f93\u51fa\uff1a

Static block initialized.
x = 42
a = 3
b = 12
\u5728\u5b9a\u4e49\u5b83\u4eec\u7684\u7c7b\u7684\u5916\u9762\uff0cstatic \u65b9\u6cd5\u548c\u53d8\u91cf\u80fd\u72ec\u7acb\u4e8e\u4efb\u4f55\u5bf9\u8c61\u800c\u88ab\u4f7f\u7528\u3002\u8fd9\u6837\uff0c\u4f60\u53ea\u8981\u5728\u7c7b\u7684\u540d\u5b57\u540e\u9762\u52a0\u70b9\u53f7\u8fd0\u7b97\u7b26\u5373\u53ef\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u5e0c\u671b\u4ece\u7c7b\u5916\u9762\u8c03\u7528\u4e00\u4e2astatic\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4e0b\u9762\u901a\u7528\u7684\u683c\u5f0f\uff1a

classname.method( )

\u8fd9\u91cc\uff0cclassname \u662f\u7c7b\u7684\u540d\u5b57\uff0c\u5728\u8be5\u7c7b\u4e2d\u5b9a\u4e49static\u65b9\u6cd5\u3002\u53ef\u4ee5\u770b\u5230\uff0c\u8fd9\u79cd\u683c\u5f0f\u4e0e\u901a\u8fc7\u5bf9\u8c61\u5f15\u7528\u53d8\u91cf\u8c03\u7528\u975estatic\u65b9\u6cd5\u7684\u683c\u5f0f\u7c7b\u4f3c\u3002\u4e00\u4e2astatic\u53d8\u91cf\u53ef\u4ee5\u4ee5\u540c\u6837\u7684\u683c\u5f0f\u6765\u8bbf\u95ee\u2014\u2014\u7c7b\u540d\u52a0\u70b9\u53f7\u8fd0\u7b97\u7b26\u3002\u8fd9\u5c31\u662fJava \u5982\u4f55\u5b9e\u73b0\u5168\u5c40\u529f\u80fd\u548c\u5168\u5c40\u53d8\u91cf\u7684\u4e00\u4e2a\u63a7\u5236\u7248\u672c\u3002

\u4e0b\u9762\u662f\u4e00\u4e2a\u4f8b\u5b50\u3002\u5728main() \u4e2d\uff0cstatic\u65b9\u6cd5callme() \u548cstatic \u53d8\u91cfb\u5728\u5b83\u4eec\u7684\u7c7b\u4e4b\u5916\u88ab\u8bbf\u95ee\u3002

class StaticDemo {
static int a = 42;
static int b = 99;
static void callme() {

System.out.println("a = " + a);
}
}

class StaticByName {

public static void main(String args[]) {
StaticDemo.callme();
System.out.println("b = " + StaticDemo.b);
}
}

\u4e0b\u9762\u662f\u8be5\u7a0b\u5e8f\u7684\u8f93\u51fa\uff1a

a = 42
b = 99

static\u6210\u5458\u662f\u4e0d\u80fd\u88ab\u5176\u6240\u5728class\u521b\u5efa\u7684\u5b9e\u4f8b\u8bbf\u95ee\u7684\u3002

\u5982\u679c\u4e0d\u52a0static\u4fee\u9970\u7684\u6210\u5458\u662f\u5bf9\u8c61\u6210\u5458\uff0c\u4e5f\u5c31\u662f\u5f52\u6bcf\u4e2a\u5bf9\u8c61\u6240\u6709\u7684\u3002

\u52a0static\u4fee\u9970\u7684\u6210\u5458\u662f\u7c7b\u6210\u5458\uff0c\u5c31\u662f\u53ef\u4ee5\u7531\u4e00\u4e2a\u7c7b\u76f4\u63a5\u8c03\u7528\uff0c\u4e3a\u6240\u6709\u5bf9\u8c61\u5171\u6709\u7684

static\u8868\u793a\u201c\u5168\u5c40\u201d\u6216\u8005\u201c\u9759\u6001\u201d\u7684\u610f\u601d\uff0c\u7528\u6765\u4fee\u9970\u6210\u5458\u53d8\u91cf\u548c\u6210\u5458\u65b9\u6cd5\uff0c\u4e5f\u53ef\u4ee5\u5f62\u6210\u9759\u6001static\u4ee3\u7801\u5757\uff0c\u4f46\u662fJava\u8bed\u8a00\u4e2d\u6ca1\u6709\u5168\u5c40\u53d8\u91cf\u7684\u6982\u5ff5\u3002
\u88abstatic\u4fee\u9970\u7684\u6210\u5458\u53d8\u91cf\u548c\u6210\u5458\u65b9\u6cd5\u72ec\u7acb\u4e8e\u8be5\u7c7b\u7684\u4efb\u4f55\u5bf9\u8c61\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u5b83\u4e0d\u4f9d\u8d56\u7c7b\u7279\u5b9a\u7684\u5b9e\u4f8b\uff0c\u88ab\u7c7b\u7684\u6240\u6709\u5b9e\u4f8b\u5171\u4eab\u3002
\u53ea\u8981\u8fd9\u4e2a\u7c7b\u88ab\u52a0\u8f7d\uff0cJava\u865a\u62df\u673a\u5c31\u80fd\u6839\u636e\u7c7b\u540d\u5728\u8fd0\u884c\u65f6\u6570\u636e\u533a\u7684\u65b9\u6cd5\u533a\u5185\u5b9a\u627e\u5230\u4ed6\u4eec\u3002\u56e0\u6b64\uff0cstatic\u5bf9\u8c61\u53ef\u4ee5\u5728\u5b83\u7684\u4efb\u4f55\u5bf9\u8c61\u521b\u5efa\u4e4b\u524d\u8bbf\u95ee\uff0c\u65e0\u9700\u5f15\u7528\u4efb\u4f55\u5bf9\u8c61\u3002
\u7528public\u4fee\u9970\u7684static\u6210\u5458\u53d8\u91cf\u548c\u6210\u5458\u65b9\u6cd5\u672c\u8d28\u662f\u5168\u5c40\u53d8\u91cf\u548c\u5168\u5c40\u65b9\u6cd5\uff0c\u5f53\u58f0\u660e\u5b83\u7c7b\u7684\u5bf9\u8c61\u5e02\uff0c\u4e0d\u751f\u6210static\u53d8\u91cf\u7684\u526f\u672c\uff0c\u800c\u662f\u7c7b\u7684\u6240\u6709\u5b9e\u4f8b\u5171\u4eab\u540c\u4e00\u4e2astatic\u53d8\u91cf\u3002
static\u53d8\u91cf\u524d\u53ef\u4ee5\u6709private\u4fee\u9970\uff0c\u8868\u793a\u8fd9\u4e2a\u53d8\u91cf\u53ef\u4ee5\u5728\u7c7b\u7684\u9759\u6001\u4ee3\u7801\u5757\u4e2d\uff0c\u6216\u8005\u7c7b\u7684\u5176\u4ed6\u9759\u6001\u6210\u5458\u65b9\u6cd5\u4e2d\u4f7f\u7528\uff08\u5f53\u7136\u4e5f\u53ef\u4ee5\u5728\u975e\u9759\u6001\u6210\u5458\u65b9\u6cd5\u4e2d\u4f7f\u7528--\u5e9f\u8bdd\uff09\uff0c\u4f46\u662f\u4e0d\u80fd\u5728\u5176\u4ed6\u7c7b\u4e2d\u901a\u8fc7\u7c7b\u540d\u6765\u76f4\u63a5\u5f15\u7528\uff0c\u8fd9\u4e00\u70b9\u5f88\u91cd\u8981\u3002\u5b9e\u9645\u4e0a\u4f60\u9700\u8981\u641e\u660e\u767d\uff0cprivate\u662f\u8bbf\u95ee\u6743\u9650\u9650\u5b9a\uff0cstatic\u8868\u793a\u4e0d\u8981\u5b9e\u4f8b\u5316\u5c31\u53ef\u4ee5\u4f7f\u7528\uff0c\u8fd9\u6837\u5c31\u5bb9\u6613\u7406\u89e3\u591a\u4e86\u3002static\u524d\u9762\u52a0\u4e0a\u5176\u5b83\u8bbf\u95ee\u6743\u9650\u5173\u952e\u5b57\u7684\u6548\u679c\u4e5f\u4ee5\u6b64\u7c7b\u63a8\u3002
static\u4fee\u9970\u7684\u6210\u5458\u53d8\u91cf\u548c\u6210\u5458\u65b9\u6cd5\u4e60\u60ef\u4e0a\u79f0\u4e3a\u9759\u6001\u53d8\u91cf\u548c\u9759\u6001\u65b9\u6cd5\uff0c\u53ef\u4ee5\u76f4\u63a5\u901a\u8fc7\u7c7b\u540d\u6765\u8bbf\u95ee\uff0c\u8bbf\u95ee\u8bed\u6cd5\u4e3a\uff1a
\u7c7b\u540d.\u9759\u6001\u65b9\u6cd5\u540d(\u53c2\u6570\u5217\u8868...)
\u7c7b\u540d.\u9759\u6001\u53d8\u91cf\u540d
\u7528static\u4fee\u9970\u7684\u4ee3\u7801\u5757\u8868\u793a\u9759\u6001\u4ee3\u7801\u5757\uff0c\u5f53Java\u865a\u62df\u673a\uff08JVM\uff09\u52a0\u8f7d\u7c7b\u65f6\uff0c\u5c31\u4f1a\u6267\u884c\u8be5\u4ee3\u7801\u5757\uff08\u7528\u5904\u975e\u5e38\u5927\uff0c\u5475\u5475\uff09\u3002
1\u3001static\u53d8\u91cf
\u6309\u7167\u662f\u5426\u9759\u6001\u7684\u5bf9\u7c7b\u6210\u5458\u53d8\u91cf\u8fdb\u884c\u5206\u7c7b\u53ef\u5206\u4e24\u79cd\uff1a\u4e00\u79cd\u662f\u88abstatic\u4fee\u9970\u7684\u53d8\u91cf\uff0c\u53eb\u9759\u6001\u53d8\u91cf\u6216\u7c7b\u53d8\u91cf\uff1b\u53e6\u4e00\u79cd\u662f\u6ca1\u6709\u88abstatic\u4fee\u9970\u7684\u53d8\u91cf\uff0c\u53eb\u5b9e\u4f8b\u53d8\u91cf\u3002
\u4e24\u8005\u7684\u533a\u522b\u662f\uff1a
\u5bf9\u4e8e\u9759\u6001\u53d8\u91cf\u5728\u5185\u5b58\u4e2d\u53ea\u6709\u4e00\u4e2a\u62f7\u8d1d\uff08\u8282\u7701\u5185\u5b58\uff09\uff0cJVM\u53ea\u4e3a\u9759\u6001\u5206\u914d\u4e00\u6b21\u5185\u5b58\uff0c\u5728\u52a0\u8f7d\u7c7b\u7684\u8fc7\u7a0b\u4e2d\u5b8c\u6210\u9759\u6001\u53d8\u91cf\u7684\u5185\u5b58\u5206\u914d\uff0c\u53ef\u7528\u7c7b\u540d\u76f4\u63a5\u8bbf\u95ee\uff08\u65b9\u4fbf\uff09\uff0c\u5f53\u7136\u4e5f\u53ef\u4ee5\u901a\u8fc7\u5bf9\u8c61\u6765\u8bbf\u95ee\uff08\u4f46\u662f\u8fd9\u662f\u4e0d\u63a8\u8350\u7684\uff09\u3002
\u5bf9\u4e8e\u5b9e\u4f8b\u53d8\u91cf\uff0c\u6ca1\u521b\u5efa\u4e00\u4e2a\u5b9e\u4f8b\uff0c\u5c31\u4f1a\u4e3a\u5b9e\u4f8b\u53d8\u91cf\u5206\u914d\u4e00\u6b21\u5185\u5b58\uff0c\u5b9e\u4f8b\u53d8\u91cf\u53ef\u4ee5\u5728\u5185\u5b58\u4e2d\u6709\u591a\u4e2a\u62f7\u8d1d\uff0c\u4e92\u4e0d\u5f71\u54cd\uff08\u7075\u6d3b\uff09\u3002
\u6240\u4ee5\u4e00\u822c\u5728\u9700\u8981\u5b9e\u73b0\u4ee5\u4e0b\u4e24\u4e2a\u529f\u80fd\u65f6\u4f7f\u7528\u9759\u6001\u53d8\u91cf\uff1a
 \u5728\u5bf9\u8c61\u4e4b\u95f4\u5171\u4eab\u503c\u65f6
 \u65b9\u4fbf\u8bbf\u95ee\u53d8\u91cf\u65f6
2\u3001\u9759\u6001\u65b9\u6cd5
\u9759\u6001\u65b9\u6cd5\u53ef\u4ee5\u76f4\u63a5\u901a\u8fc7\u7c7b\u540d\u8c03\u7528\uff0c\u4efb\u4f55\u7684\u5b9e\u4f8b\u4e5f\u90fd\u53ef\u4ee5\u8c03\u7528\uff0c
\u56e0\u6b64\u9759\u6001\u65b9\u6cd5\u4e2d\u4e0d\u80fd\u7528this\u548csuper\u5173\u952e\u5b57\uff0c\u4e0d\u80fd\u76f4\u63a5\u8bbf\u95ee\u6240\u5c5e\u7c7b\u7684\u5b9e\u4f8b\u53d8\u91cf\u548c\u5b9e\u4f8b\u65b9\u6cd5(\u5c31\u662f\u4e0d\u5e26static\u7684\u6210\u5458\u53d8\u91cf\u548c\u6210\u5458\u6210\u5458\u65b9\u6cd5)\uff0c\u53ea\u80fd\u8bbf\u95ee\u6240\u5c5e\u7c7b\u7684\u9759\u6001\u6210\u5458\u53d8\u91cf\u548c\u6210\u5458\u65b9\u6cd5\u3002
\u56e0\u4e3a\u5b9e\u4f8b\u6210\u5458\u4e0e\u7279\u5b9a\u7684\u5bf9\u8c61\u5173\u8054\uff01\u8fd9\u4e2a\u9700\u8981\u53bb\u7406\u89e3\uff0c\u60f3\u660e\u767d\u5176\u4e2d\u7684\u9053\u7406\uff0c\u4e0d\u662f\u8bb0\u5fc6\uff01\uff01\uff01
\u56e0\u4e3astatic\u65b9\u6cd5\u72ec\u7acb\u4e8e\u4efb\u4f55\u5b9e\u4f8b\uff0c\u56e0\u6b64static\u65b9\u6cd5\u5fc5\u987b\u88ab\u5b9e\u73b0\uff0c\u800c\u4e0d\u80fd\u662f\u62bd\u8c61\u7684abstract\u3002
\u4f8b\u5982\u4e3a\u4e86\u65b9\u4fbf\u65b9\u6cd5\u7684\u8c03\u7528\uff0cJava API\u4e2d\u7684Math\u7c7b\u4e2d\u6240\u6709\u7684\u65b9\u6cd5\u90fd\u662f\u9759\u6001\u7684\uff0c\u800c\u4e00\u822c\u7c7b\u5185\u90e8\u7684static\u65b9\u6cd5\u4e5f\u662f\u65b9\u4fbf\u5176\u5b83\u7c7b\u5bf9\u8be5\u65b9\u6cd5\u7684\u8c03\u7528\u3002
\u9759\u6001\u65b9\u6cd5\u662f\u7c7b\u5185\u90e8\u7684\u4e00\u7c7b\u7279\u6b8a\u65b9\u6cd5\uff0c\u53ea\u6709\u5728\u9700\u8981\u65f6\u624d\u5c06\u5bf9\u5e94\u7684\u65b9\u6cd5\u58f0\u660e\u6210\u9759\u6001\u7684\uff0c\u4e00\u4e2a\u7c7b\u5185\u90e8\u7684\u65b9\u6cd5\u4e00\u822c\u90fd\u662f\u975e\u9759\u6001\u7684
3\u3001static\u4ee3\u7801\u5757
static\u4ee3\u7801\u5757\u4e5f\u53eb\u9759\u6001\u4ee3\u7801\u5757\uff0c\u662f\u5728\u7c7b\u4e2d\u72ec\u7acb\u4e8e\u7c7b\u6210\u5458\u7684static\u8bed\u53e5\u5757\uff0c\u53ef\u4ee5\u6709\u591a\u4e2a\uff0c\u4f4d\u7f6e\u53ef\u4ee5\u968f\u4fbf\u653e\uff0c\u5b83\u4e0d\u5728\u4efb\u4f55\u7684\u65b9\u6cd5\u4f53\u5185\uff0cJVM\u52a0\u8f7d\u7c7b\u65f6\u4f1a\u6267\u884c\u8fd9\u4e9b\u9759\u6001\u7684\u4ee3\u7801\u5757\uff0c\u5982\u679cstatic\u4ee3\u7801\u5757\u6709\u591a\u4e2a\uff0cJVM\u5c06\u6309\u7167\u5b83\u4eec\u5728\u7c7b\u4e2d\u51fa\u73b0\u7684\u5148\u540e\u987a\u5e8f\u4f9d\u6b21\u6267\u884c\u5b83\u4eec\uff0c\u6bcf\u4e2a\u4ee3\u7801\u5757\u53ea\u4f1a\u88ab\u6267\u884c\u4e00\u6b21\u3002\u4f8b\u5982\uff1a
public class Test5 {
private static int a;
private int b;
static{
Test5.a=3;
System.out.println(a);
Test5 t=new Test5();
t.f();
t.b=1000;
System.out.println(t.b);
}
static{
Test5.a=4;
System.out.println(a);
}
public static void main(String[] args) {
// TODO \u81ea\u52a8\u751f\u6210\u65b9\u6cd5\u5b58\u6839
}
static{
Test5.a=5;
System.out.println(a);
}
public void f(){
System.out.println("hhahhahah");
}
}
\u8fd0\u884c\u7ed3\u679c\uff1a
3
hhahhahah
1000
4
5
\u5229\u7528\u9759\u6001\u4ee3\u7801\u5757\u53ef\u4ee5\u5bf9\u4e00\u4e9bstatic\u53d8\u91cf\u8fdb\u884c\u8d4b\u503c\uff0c\u6700\u540e\u518d\u770b\u4e00\u773c\u8fd9\u4e9b\u4f8b\u5b50\uff0c\u90fd\u4e00\u4e2astatic\u7684main\u65b9\u6cd5\uff0c\u8fd9\u6837JVM\u5728\u8fd0\u884cmain\u65b9\u6cd5\u7684\u65f6\u5019\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528\u800c\u4e0d\u7528\u521b\u5efa\u5b9e\u4f8b\u3002
4\u3001static\u548cfinal\u4e00\u5757\u7528\u8868\u793a\u4ec0\u4e48
static final\u7528\u6765\u4fee\u9970\u6210\u5458\u53d8\u91cf\u548c\u6210\u5458\u65b9\u6cd5\uff0c\u53ef\u7b80\u5355\u7406\u89e3\u4e3a\u201c\u5168\u5c40\u5e38\u91cf\u201d\uff01
\u5bf9\u4e8e\u53d8\u91cf\uff0c\u8868\u793a\u4e00\u65e6\u7ed9\u503c\u5c31\u4e0d\u53ef\u4fee\u6539\uff0c\u5e76\u4e14\u901a\u8fc7\u7c7b\u540d\u53ef\u4ee5\u8bbf\u95ee\u3002
\u5bf9\u4e8e\u65b9\u6cd5\uff0c\u8868\u793a\u4e0d\u53ef\u8986\u76d6\uff0c\u5e76\u4e14\u53ef\u4ee5\u901a\u8fc7\u7c7b\u540d\u76f4\u63a5\u8bbf\u95ee\u3002
\u6709\u65f6\u4f60\u5e0c\u671b\u5b9a\u4e49\u4e00\u4e2a\u7c7b\u6210\u5458\uff0c\u4f7f\u5b83\u7684\u4f7f\u7528\u5b8c\u5168\u72ec\u7acb\u4e8e\u8be5\u7c7b\u7684\u4efb\u4f55\u5bf9\u8c61\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u7c7b\u6210\u5458\u5fc5\u987b\u901a\u8fc7\u5b83\u7684\u7c7b\u7684\u5bf9\u8c61\u8bbf\u95ee\uff0c\u4f46\u662f\u53ef\u4ee5\u521b\u5efa\u8fd9\u6837\u4e00\u4e2a\u6210\u5458\uff0c\u5b83\u80fd\u591f\u88ab\u5b83\u81ea\u5df1\u4f7f\u7528\uff0c\u800c\u4e0d\u5fc5\u5f15\u7528\u7279\u5b9a\u7684\u5b9e\u4f8b\u3002\u5728\u6210\u5458\u7684\u58f0\u660e\u524d\u9762\u52a0\u4e0a\u5173\u952e\u5b57static(\u9759\u6001\u7684)\u5c31\u80fd\u521b\u5efa\u8fd9\u6837\u7684\u6210\u5458\u3002\u5982\u679c\u4e00\u4e2a\u6210\u5458\u88ab\u58f0\u660e\u4e3astatic\uff0c\u5b83\u5c31\u80fd\u591f\u5728\u5b83\u7684\u7c7b\u7684\u4efb\u4f55\u5bf9\u8c61\u521b\u5efa\u4e4b\u524d\u88ab\u8bbf\u95ee\uff0c\u800c\u4e0d\u5fc5\u5f15\u7528\u4efb\u4f55\u5bf9\u8c61\u3002\u4f60\u53ef\u4ee5\u5c06\u65b9\u6cd5\u548c\u53d8\u91cf\u90fd\u58f0\u660e\u4e3astatic\u3002static \u6210\u5458\u7684\u6700\u5e38\u89c1\u7684\u4f8b\u5b50\u662fmain( ) \u3002\u56e0\u4e3a\u5728\u7a0b\u5e8f\u5f00\u59cb\u6267\u884c\u65f6\u5fc5\u987b\u8c03\u7528main() \uff0c\u6240\u4ee5\u5b83\u88ab\u58f0\u660e\u4e3astatic\u3002
\u58f0\u660e\u4e3astatic\u7684\u53d8\u91cf\u5b9e\u8d28\u4e0a\u5c31\u662f\u5168\u5c40\u53d8\u91cf\u3002\u5f53\u58f0\u660e\u4e00\u4e2a\u5bf9\u8c61\u65f6\uff0c\u5e76\u4e0d\u4ea7\u751fstatic\u53d8\u91cf\u7684\u62f7\u8d1d\uff0c\u800c\u662f\u8be5\u7c7b\u6240\u6709\u7684\u5b9e\u4f8b\u53d8\u91cf\u5171\u7528\u540c\u4e00\u4e2astatic\u53d8\u91cf\u3002\u58f0\u660e\u4e3astatic\u7684\u65b9\u6cd5\u6709\u4ee5\u4e0b\u51e0\u6761\u9650\u5236\uff1a

\u5b83\u4eec\u4ec5\u80fd\u8c03\u7528\u5176\u4ed6\u7684static \u65b9\u6cd5\u3002

\u5b83\u4eec\u53ea\u80fd\u8bbf\u95eestatic\u6570\u636e\u3002

\u5b83\u4eec\u4e0d\u80fd\u4ee5\u4efb\u4f55\u65b9\u5f0f\u5f15\u7528this \u6216super\uff08\u5173\u952e\u5b57super \u4e0e\u7ee7\u627f\u6709\u5173\uff0c\u5728\u4e0b\u4e00\u7ae0\u4e2d\u63cf\u8ff0\uff09\u3002
\u5982\u679c\u4f60\u9700\u8981\u901a\u8fc7\u8ba1\u7b97\u6765\u521d\u59cb\u5316\u4f60\u7684static\u53d8\u91cf\uff0c\u4f60\u53ef\u4ee5\u58f0\u660e\u4e00\u4e2astatic\u5757\uff0cStatic \u5757\u4ec5\u5728\u8be5\u7c7b\u88ab\u52a0\u8f7d\u65f6\u6267\u884c\u4e00\u6b21\u3002\u4e0b\u9762\u7684\u4f8b\u5b50\u663e\u793a\u7684\u7c7b\u6709\u4e00\u4e2astatic\u65b9\u6cd5\uff0c\u4e00\u4e9bstatic\u53d8\u91cf\uff0c\u4ee5\u53ca\u4e00\u4e2astatic \u521d\u59cb\u5316\u5757\uff1a
// Demonstrate static variables\uff0cmethods\uff0cand blocks.
class UseStatic {
static int a = 3;
static int b;
static void meth(int x) {
System.out.println("x = " + x);
System.out.println("a = " + a);
System.out.println("b = " + b);
}
static {
System.out.println("Static block initialized.");
b = a * 4;
}
public static void main(String args[]) {
meth(42);
}
}
\u4e00\u65e6UseStatic \u7c7b\u88ab\u88c5\u8f7d\uff0c\u6240\u6709\u7684static\u8bed\u53e5\u88ab\u8fd0\u884c\u3002\u9996\u5148\uff0ca\u88ab\u8bbe\u7f6e\u4e3a3\uff0c\u63a5\u7740static \u5757\u6267\u884c(\u6253\u5370\u4e00\u6761\u6d88\u606f)\uff0c\u6700\u540e\uff0cb\u88ab\u521d\u59cb\u5316\u4e3aa*4 \u621612\u3002\u7136\u540e\u8c03\u7528main()\uff0cmain() \u8c03\u7528meth() \uff0c\u628a\u503c42\u4f20\u9012\u7ed9x\u30023\u4e2aprintln ( ) \u8bed\u53e5\u5f15\u7528\u4e24\u4e2astatic\u53d8\u91cfa\u548cb\uff0c\u4ee5\u53ca\u5c40\u90e8\u53d8\u91cfx \u3002
\u6ce8\u610f\uff1a\u5728\u4e00\u4e2astatic \u65b9\u6cd5\u4e2d\u5f15\u7528\u4efb\u4f55\u5b9e\u4f8b\u53d8\u91cf\u90fd\u662f\u975e\u6cd5\u7684\u3002
\u4e0b\u9762\u662f\u8be5\u7a0b\u5e8f\u7684\u8f93\u51fa\uff1a
Static block initialized.
x = 42
a = 3
b = 12
\u5728\u5b9a\u4e49\u5b83\u4eec\u7684\u7c7b\u7684\u5916\u9762\uff0cstatic \u65b9\u6cd5\u548c\u53d8\u91cf\u80fd\u72ec\u7acb\u4e8e\u4efb\u4f55\u5bf9\u8c61\u800c\u88ab\u4f7f\u7528\u3002\u8fd9\u6837\uff0c\u4f60\u53ea\u8981\u5728\u7c7b\u7684\u540d\u5b57\u540e\u9762\u52a0\u70b9\u53f7\u8fd0\u7b97\u7b26\u5373\u53ef\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u5e0c\u671b\u4ece\u7c7b\u5916\u9762\u8c03\u7528\u4e00\u4e2astatic\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4e0b\u9762\u901a\u7528\u7684\u683c\u5f0f\uff1a
classname.method( )
\u8fd9\u91cc\uff0cclassname \u662f\u7c7b\u7684\u540d\u5b57\uff0c\u5728\u8be5\u7c7b\u4e2d\u5b9a\u4e49static\u65b9\u6cd5\u3002\u53ef\u4ee5\u770b\u5230\uff0c\u8fd9\u79cd\u683c\u5f0f\u4e0e\u901a\u8fc7\u5bf9\u8c61\u5f15\u7528\u53d8\u91cf\u8c03\u7528\u975estatic\u65b9\u6cd5\u7684\u683c\u5f0f\u7c7b\u4f3c\u3002\u4e00\u4e2astatic\u53d8\u91cf\u53ef\u4ee5\u4ee5\u540c\u6837\u7684\u683c\u5f0f\u6765\u8bbf\u95ee\u2014\u2014\u7c7b\u540d\u52a0\u70b9\u53f7\u8fd0\u7b97\u7b26\u3002\u8fd9\u5c31\u662fJava \u5982\u4f55\u5b9e\u73b0\u5168\u5c40\u529f\u80fd\u548c\u5168\u5c40\u53d8\u91cf\u7684\u4e00\u4e2a\u63a7\u5236\u7248\u672c\u3002
\u4e0b\u9762\u662f\u4e00\u4e2a\u4f8b\u5b50\u3002\u5728main() \u4e2d\uff0cstatic\u65b9\u6cd5callme() \u548cstatic \u53d8\u91cfb\u5728\u5b83\u4eec\u7684\u7c7b\u4e4b\u5916\u88ab\u8bbf\u95ee\u3002
class StaticDemo {
static int a = 42;
static int b = 99;
static void callme() {
System.out.println("a = " + a);
}
}
class StaticByName {
public static void main(String args[]) {
StaticDemo.callme();
System.out.println("b = " + StaticDemo.b);
}
}
\u4e0b\u9762\u662f\u8be5\u7a0b\u5e8f\u7684\u8f93\u51fa\uff1a
a = 42
b = 99
static\u6210\u5458\u662f\u4e0d\u80fd\u88ab\u5176\u6240\u5728class\u521b\u5efa\u7684\u5b9e\u4f8b\u8bbf\u95ee\u7684\u3002
\u5982\u679c\u4e0d\u52a0static\u4fee\u9970\u7684\u6210\u5458\u662f\u5bf9\u8c61\u6210\u5458\uff0c\u4e5f\u5c31\u662f\u5f52\u6bcf\u4e2a\u5bf9\u8c61\u6240\u6709\u7684\u3002
\u52a0static\u4fee\u9970\u7684\u6210\u5458\u662f\u7c7b\u6210\u5458\uff0c\u5c31\u662f\u53ef\u4ee5\u7531\u4e00\u4e2a\u7c7b\u76f4\u63a5\u8c03\u7528\uff0c\u4e3a\u6240\u6709\u5bf9\u8c61\u5171\u6709\u7684

作用于代码块上时:形成静态代码块以优化程序性能。因为它的特性:只会在类加载的时候执行一次。

   [1]要创建嵌套类的对象,并不需要其外围类的对象;

   [2]不能从嵌套类的对象中访问非静态的外围类对象(不能够从静态内部类的对象中访问外部类的非静态成员);

另外,在创建静态内部类时不需要将静态内部类的实例绑定在外部类的实例上。普通非静态内部类的对象是依附在外部类对象之中的,要在一个外部类中定义一个静态的内部类,不需要利用关键字new来创建内部类的实例。静态类和方法只属于类本身,并不属于该类的对象,更不属于其他外部类的对象。



扩展阅读:java serializable ... javascript void ... java static方法 ... java入门网站 ... java static关键字的作用 ... java static变量 ... java statement ... java static可以修饰 ... java中static的作用和用法 ...

本站交流只代表网友个人观点,与本站立场无关
欢迎反馈与建议,请联系电邮
2024© 车视网