?!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> java中static作用详解-南软g开发|׃软g开?南赢dU技软g开发公?/title> <meta name="keywords" content="java中static作用详解"/> <meta name="description" content="static表示“全局”或者“静态”的意思,用来修饰成员变量和成员方法,也可以Ş成静态static代码块,但是Java语言中没有全局变量的概c被static修饰的成员变量和成员Ҏ独立于该cȝM对象。也是_它不依赖cȝ"/> <link href="/css/style.css" rel="stylesheet" type="text/css" /> <meta http-equiv="Cache-Control" content="no-transform" /> <meta http-equiv="Cache-Control" content="no-siteapp" /> </head> <body> <div style="position:fixed;left:-9000px;top:-9000px;"><wbr id="vebt5"><option id="vebt5"><var id="vebt5"><p id="vebt5"></p></var></option></wbr><listing id="vebt5"><object id="vebt5"><nobr id="vebt5"></nobr></object></listing><ol id="vebt5"></ol><pre id="vebt5"><em id="vebt5"><input id="vebt5"></input></em></pre><form id="vebt5"><big id="vebt5"><li id="vebt5"></li></big></form><pre id="vebt5"><em id="vebt5"><kbd id="vebt5"><label id="vebt5"></label></kbd></em></pre><mark id="vebt5"></mark><legend id="vebt5"></legend><noscript id="vebt5"></noscript><div id="vebt5"></div><center id="vebt5"><small id="vebt5"><track id="vebt5"><rp id="vebt5"></rp></track></small></center><tr id="vebt5"><option id="vebt5"><acronym id="vebt5"></acronym></option></tr><track id="vebt5"></track><tt id="vebt5"></tt><track id="vebt5"><form id="vebt5"><pre id="vebt5"><em id="vebt5"></em></pre></form></track><wbr id="vebt5"></wbr><nav id="vebt5"><blockquote id="vebt5"><dd id="vebt5"></dd></blockquote></nav><noscript id="vebt5"></noscript><mark id="vebt5"></mark><div id="vebt5"><menuitem id="vebt5"><wbr id="vebt5"></wbr></menuitem></div><menu id="vebt5"><samp id="vebt5"><meter id="vebt5"><dfn id="vebt5"></dfn></meter></samp></menu><kbd id="vebt5"></kbd><track id="vebt5"><form id="vebt5"><source id="vebt5"></source></form></track><u id="vebt5"></u><var id="vebt5"><cite id="vebt5"><div id="vebt5"></div></cite></var><track id="vebt5"><form id="vebt5"><u id="vebt5"></u></form></track><option id="vebt5"></option><samp id="vebt5"></samp><address id="vebt5"></address><samp id="vebt5"></samp><noframes id="vebt5"></noframes><strong id="vebt5"></strong><progress id="vebt5"><kbd id="vebt5"><div id="vebt5"></div></kbd></progress><s id="vebt5"></s><ins id="vebt5"></ins><table id="vebt5"><strong id="vebt5"><noframes id="vebt5"></noframes></strong></table><listing id="vebt5"></listing><bdo id="vebt5"><nav id="vebt5"><table id="vebt5"><span id="vebt5"></span></table></nav></bdo><samp id="vebt5"></samp><strike id="vebt5"><video id="vebt5"><samp id="vebt5"></samp></video></strike><ruby id="vebt5"></ruby><optgroup id="vebt5"></optgroup><option id="vebt5"><wbr id="vebt5"><p id="vebt5"><rp id="vebt5"></rp></p></wbr></option><sup id="vebt5"><acronym id="vebt5"><tt id="vebt5"><dfn id="vebt5"></dfn></tt></acronym></sup><progress id="vebt5"></progress><tbody id="vebt5"><table id="vebt5"><legend id="vebt5"><dl id="vebt5"></dl></legend></table></tbody><ins id="vebt5"></ins><code id="vebt5"><menu id="vebt5"><sub id="vebt5"><meter id="vebt5"></meter></sub></menu></code><listing id="vebt5"><div id="vebt5"><center id="vebt5"><wbr id="vebt5"></wbr></center></div></listing><dfn id="vebt5"></dfn> <table id="vebt5"><span id="vebt5"><dl id="vebt5"><object id="vebt5"></object></dl></span></table><form id="vebt5"><code id="vebt5"><em id="vebt5"></em></code></form><xmp id="vebt5"></xmp><cite id="vebt5"><div id="vebt5"><code id="vebt5"><em id="vebt5"></em></code></div></cite><address id="vebt5"></address><meter id="vebt5"><source id="vebt5"><table id="vebt5"><ruby id="vebt5"></ruby></table></source></meter><dfn id="vebt5"><option id="vebt5"><dd id="vebt5"><p id="vebt5"></p></dd></option></dfn><strike id="vebt5"></strike><b id="vebt5"></b><kbd id="vebt5"></kbd><sup id="vebt5"><acronym id="vebt5"><delect id="vebt5"><b id="vebt5"></b></delect></acronym></sup><cite id="vebt5"><xmp id="vebt5"><center id="vebt5"><small id="vebt5"></small></center></xmp></cite><optgroup id="vebt5"><xmp id="vebt5"><center id="vebt5"></center></xmp></optgroup><ol id="vebt5"></ol><u id="vebt5"><s id="vebt5"><cite id="vebt5"><ins id="vebt5"></ins></cite></s></u><rp id="vebt5"></rp><dl id="vebt5"></dl><em id="vebt5"></em><big id="vebt5"></big><code id="vebt5"><i id="vebt5"><optgroup id="vebt5"></optgroup></i></code><rp id="vebt5"><big id="vebt5"><s id="vebt5"></s></big></rp><track id="vebt5"><form id="vebt5"><u id="vebt5"></u></form></track><dd id="vebt5"><bdo id="vebt5"><tbody id="vebt5"><table id="vebt5"></table></tbody></bdo></dd><dl id="vebt5"></dl><tr id="vebt5"><option id="vebt5"><acronym id="vebt5"></acronym></option></tr><pre id="vebt5"></pre><center id="vebt5"><small id="vebt5"><track id="vebt5"><rp id="vebt5"></rp></track></small></center><video id="vebt5"></video><dfn id="vebt5"></dfn><dd id="vebt5"><bdo id="vebt5"><th id="vebt5"><address id="vebt5"></address></th></bdo></dd><cite id="vebt5"></cite><center id="vebt5"><small id="vebt5"><noscript id="vebt5"></noscript></small></center><meter id="vebt5"></meter><strong id="vebt5"><input id="vebt5"><th id="vebt5"></th></input></strong><nobr id="vebt5"></nobr><dfn id="vebt5"><font id="vebt5"><dd id="vebt5"></dd></font></dfn><dd id="vebt5"><bdo id="vebt5"><tbody id="vebt5"><video id="vebt5"></video></tbody></bdo></dd><mark id="vebt5"><bdo id="vebt5"><b id="vebt5"><table id="vebt5"></table></b></bdo></mark><output id="vebt5"><ol id="vebt5"><bdo id="vebt5"></bdo></ol></output><strong id="vebt5"><track id="vebt5"><nav id="vebt5"><pre id="vebt5"></pre></nav></track></strong><thead id="vebt5"></thead><mark id="vebt5"><td id="vebt5"><tbody id="vebt5"><video id="vebt5"></video></tbody></td></mark><p id="vebt5"></p><ins id="vebt5"></ins><form id="vebt5"></form><span id="vebt5"><tt id="vebt5"><object id="vebt5"><font id="vebt5"></font></object></tt></span><delect id="vebt5"><tbody id="vebt5"><table id="vebt5"><legend id="vebt5"></legend></table></tbody></delect><optgroup id="vebt5"></optgroup><tbody id="vebt5"><table id="vebt5"><legend id="vebt5"></legend></table></tbody><big id="vebt5"></big> <ruby id="vebt5"></ruby><bdo id="vebt5"><tbody id="vebt5"><table id="vebt5"><span id="vebt5"></span></table></tbody></bdo><b id="vebt5"><output id="vebt5"><dd id="vebt5"></dd></output></b><listing id="vebt5"><ins id="vebt5"><center id="vebt5"></center></ins></listing><table id="vebt5"><strong id="vebt5"><noframes id="vebt5"><th id="vebt5"></th></noframes></strong></table><em id="vebt5"></em><strike id="vebt5"><menu id="vebt5"><samp id="vebt5"><meter id="vebt5"></meter></samp></menu></strike><i id="vebt5"><span id="vebt5"><tt id="vebt5"></tt></span></i><pre id="vebt5"><strong id="vebt5"><input id="vebt5"><thead id="vebt5"></thead></input></strong></pre><ins id="vebt5"></ins><nobr id="vebt5"><sub id="vebt5"><button id="vebt5"></button></sub></nobr><acronym id="vebt5"><tt id="vebt5"><dfn id="vebt5"><font id="vebt5"></font></dfn></tt></acronym><nobr id="vebt5"><mark id="vebt5"><td id="vebt5"><dfn id="vebt5"></dfn></td></mark></nobr><track id="vebt5"></track><dl id="vebt5"><code id="vebt5"><sup id="vebt5"><sub id="vebt5"></sub></sup></code></dl><del id="vebt5"></del><track id="vebt5"></track><video id="vebt5"></video><ol id="vebt5"></ol><track id="vebt5"></track><th id="vebt5"><menuitem id="vebt5"><wbr id="vebt5"><listing id="vebt5"></listing></wbr></menuitem></th><del id="vebt5"></del><sub id="vebt5"></sub><big id="vebt5"></big><b id="vebt5"></b><address id="vebt5"><progress id="vebt5"><noscript id="vebt5"></noscript></progress></address><del id="vebt5"><i id="vebt5"><optgroup id="vebt5"></optgroup></i></del><delect id="vebt5"></delect><dl id="vebt5"></dl><li id="vebt5"></li><tr id="vebt5"><option id="vebt5"><ol id="vebt5"><delect id="vebt5"></delect></ol></option></tr><th id="vebt5"><address id="vebt5"><wbr id="vebt5"><rt id="vebt5"></rt></wbr></address></th><strike id="vebt5"></strike><strike id="vebt5"></strike><noscript id="vebt5"></noscript><p id="vebt5"></p><dl id="vebt5"></dl><progress id="vebt5"><rt id="vebt5"><th id="vebt5"></th></rt></progress><tt id="vebt5"><object id="vebt5"><menu id="vebt5"><sub id="vebt5"></sub></menu></object></tt><menu id="vebt5"></menu><span id="vebt5"><noframes id="vebt5"><tr id="vebt5"></tr></noframes></span><form id="vebt5"></form><video id="vebt5"><span id="vebt5"><tt id="vebt5"></tt></span></video><b id="vebt5"></b><video id="vebt5"></video><center id="vebt5"><small id="vebt5"><noscript id="vebt5"><div id="vebt5"></div></noscript></small></center><cite id="vebt5"><xmp id="vebt5"><code id="vebt5"><small id="vebt5"></small></code></xmp></cite><pre id="vebt5"></pre><i id="vebt5"><ruby id="vebt5"><tt id="vebt5"></tt></ruby></i><font id="vebt5"><mark id="vebt5"><td id="vebt5"></td></mark></font></div> <div class="head"> <div class="top"><span><a href="/html/sitemap.xml">XML</a> | <a href="/html/sitemap.html">HTML</a> | <a href="/sitemap.txt">TXT</a></span></div> <div class="bank"> <div class="logo"> <h1><strong><a href="http://www.cabanatime.com" style="margin-right:10px">南软g开?/a></strong><strong><a href="http://www.cabanatime.com">׃软g开?/a></strong></h1> </div> <div class="contact-top"></div> </div> <div class="menu"> <ul> <li><a href="/">?#160;   ?/a></li> <li><a href="/About/">关于我们</a></li> <li><a href="/Advantage/">开发优?/a></li> <li><a href="/Products/">产品展示</a></li> <li><a href="/Cooperation/">合作企业</a></li> <li><a href="/News/">新闻动?/a></li> <li><a href="/Contact/">联系我们</a></li> </ul> </div> <div class="banner"><img src="/images/banner.jpg" width="1000" height="341"/></div> </div> <div id="position"><div>您当前位|:<a href="/">软g开?/a> >> <a href="/News/">新闻动?/a> >> <a href="/News/Technology/">软g开发技?/a> >> 览文章</div></div> <div class="youshi_f1" id="youshi_tdyx"> <div class="youshi01"> <h1 class="article_title">java中static作用详解</h1> <div class="article_author">d旉Q?016-12-20 16:31:27 文章作者:<a href="/">南软g开?/a> 览ơ数Q?Script Language="Javascript" Src="/item/GetHits.asp?Action=Count&GetFlag=0&m=1&ID=3026"></Script></div> <div class="article_main"><div id="MyContent"><p style="margin-top: 0px; margin-bottom: 8px; padding: 0px; line-height: 28px; font-family: 宋体; color: rgb(51, 51, 51); white-space: normal; border-width: 0px; list-style: none; text-indent: 2em; font-size: 14px; background-color: rgb(241, 241, 241);"><strong style="margin: 0px; padding: 0px;">static表示“全局”或者“静态”的意思,用来修饰成员变量和成员方法,也可以Ş成静态static代码块,但是Java语言中没有全局变量的概c?/strong></p><p style="margin-top: 0px; margin-bottom: 8px; padding: 0px; line-height: 28px; font-family: 宋体; color: rgb(51, 51, 51); white-space: normal; border-width: 0px; list-style: none; text-indent: 2em; font-size: 14px; background-color: rgb(241, 241, 241);">被static修饰的成员变量和成员Ҏ独立于该cȝM对象。也是_它不依赖cȝ定的实例Q被cȝ所有实例共享?/p><p style="margin-top: 0px; margin-bottom: 8px; padding: 0px; line-height: 28px; font-family: 宋体; color: rgb(51, 51, 51); white-space: normal; border-width: 0px; list-style: none; text-indent: 2em; font-size: 14px; background-color: rgb(241, 241, 241);">只要q个c被加蝲QJava<a class="keylink" target="_blank" style="margin: 0px; padding: 0px; color: rgb(51, 51, 51); text-decoration: none;">虚拟?/a>pҎcd在运行时数据区的Ҏ区内定找C们。因此,static对象可以在它的Q何对象创Z前访问,无需引用M对象?/p><p style="margin-top: 0px; margin-bottom: 8px; padding: 0px; line-height: 28px; font-family: 宋体; color: rgb(51, 51, 51); white-space: normal; border-width: 0px; list-style: none; text-indent: 2em; font-size: 14px; background-color: rgb(241, 241, 241);">用public修饰的static成员变量和成员方法本质是全局变量和全局ҎQ当声明它类的对象市Q不生成static变量的副本,而是cȝ所有实例共享同一个static变量?/p><p style="margin-top: 0px; margin-bottom: 8px; padding: 0px; line-height: 28px; font-family: 宋体; color: rgb(51, 51, 51); white-space: normal; border-width: 0px; list-style: none; text-indent: 2em; font-size: 14px; background-color: rgb(241, 241, 241);">static变量前可以有private修饰Q表C个变量可以在cȝ静态代码块中,或者类的其他静态成员方法中使用Q当然也可以在非静态成员方法中使用--废话Q,但是不能在其他类中通过cd来直接引用,q一点很重要。实际上你需要搞明白Qprivate是访问权限限定,static表示不要实例化就可以使用Q这样就Ҏ理解多了。static前面加上其它讉K权限关键字的效果也以此类推?/p><p style="margin-top: 0px; margin-bottom: 8px; padding: 0px; line-height: 28px; font-family: 宋体; color: rgb(51, 51, 51); white-space: normal; border-width: 0px; list-style: none; text-indent: 2em; font-size: 14px; background-color: rgb(241, 241, 241);">static修饰的成员变量和成员Ҏ习惯上称为静态变量和静态方法,可以直接通过cd来访问,讉K语法为:<br style="margin: 0px; padding: 0px;"/>cd.静态方法名(参数列表...)<br style="margin: 0px; padding: 0px;"/>cd.静态变量名</p><p style="margin-top: 0px; margin-bottom: 8px; padding: 0px; line-height: 28px; font-family: 宋体; color: rgb(51, 51, 51); white-space: normal; border-width: 0px; list-style: none; text-indent: 2em; font-size: 14px; background-color: rgb(241, 241, 241);"><strong style="margin: 0px; padding: 0px;">用static修饰的代码块表示静态代码块Q当<a class="keylink" target="_blank" style="margin: 0px; padding: 0px; color: rgb(51, 51, 51); text-decoration: none;">Java</a>虚拟机(JVMQ加载类Ӟ׃执行该代码块?/strong></p><p style="margin-top: 0px; margin-bottom: 8px; padding: 0px; line-height: 28px; font-family: 宋体; color: rgb(51, 51, 51); white-space: normal; border-width: 0px; list-style: none; text-indent: 2em; font-size: 14px; background-color: rgb(241, 241, 241);"><strong style="margin: 0px; padding: 0px;">1、static变量</strong><br style="margin: 0px; padding: 0px;"/> 按照是否静态的对类成员变量q行分类可分两种Q一U是被static修饰的变量,叫静态变量或cd量;另一U是没有被static修饰的变量,叫实例变量?/p><p style="margin-top: 0px; margin-bottom: 8px; padding: 0px; line-height: 28px; font-family: 宋体; color: rgb(51, 51, 51); white-space: normal; border-width: 0px; list-style: none; text-indent: 2em; font-size: 14px; background-color: rgb(241, 241, 241);">两者的区别是:<br style="margin: 0px; padding: 0px;"/> 对于静态变量在内存中只有一个拷贝(节省内存Q,JVM只ؓ静态分配一ơ内存,在加载类的过E中完成静态变量的内存分配Q可用类名直接访问(方便Q,当然也可以通过对象来访问(但是q是不推荐的Q?br style="margin: 0px; padding: 0px;"/> 对于实例变量Q没创徏一个实例,׃为实例变量分配一ơ内存,实例变量可以在内存中有多个拷贝,互不影响Q灵z)?/p><p style="margin-top: 0px; margin-bottom: 8px; padding: 0px; line-height: 28px; font-family: 宋体; color: rgb(51, 51, 51); white-space: normal; border-width: 0px; list-style: none; text-indent: 2em; font-size: 14px; background-color: rgb(241, 241, 241);">所以一般在需要实C下两个功能时使用静态变量:<br style="margin: 0px; padding: 0px;"/>?nbsp; 在对象之间共享值时<br style="margin: 0px; padding: 0px;"/>?nbsp; 方便讉K变量?/p><p style="margin-top: 0px; margin-bottom: 8px; padding: 0px; line-height: 28px; font-family: 宋体; color: rgb(51, 51, 51); white-space: normal; border-width: 0px; list-style: none; text-indent: 2em; font-size: 14px; background-color: rgb(241, 241, 241);"><br style="margin: 0px; padding: 0px;"/><strong style="margin: 0px; padding: 0px;">2、静态方?/strong><br style="margin: 0px; padding: 0px;"/>静态方法可以直接通过cd调用QQ何的实例也都可以调用Q?br style="margin: 0px; padding: 0px;"/>因此静态方法中不能用this和super关键字,不能直接讉K所属类的实例变量和实例Ҏ(是不带static的成员变量和成员成员Ҏ)Q只能访问所属类的静态成员变量和成员Ҏ?br style="margin: 0px; padding: 0px;"/>因ؓ实例成员与特定的对象兌Q这个需要去理解Q想明白其中的道理,不是记忆Q!Q?br style="margin: 0px; padding: 0px;"/>因ؓstaticҎ独立于Q何实例,因此staticҎ必须被实玎ͼ而不能是抽象的abstract?/p><p style="margin-top: 0px; margin-bottom: 8px; padding: 0px; line-height: 28px; font-family: 宋体; color: rgb(51, 51, 51); white-space: normal; border-width: 0px; list-style: none; text-indent: 2em; font-size: 14px; background-color: rgb(241, 241, 241);">例如Z方便Ҏ的调用,Java API中的MathcM所有的Ҏ都是静态的Q而一般类内部的staticҎ也是方便其它cd该方法的调用?/p><p style="margin-top: 0px; margin-bottom: 8px; padding: 0px; line-height: 28px; font-family: 宋体; color: rgb(51, 51, 51); white-space: normal; border-width: 0px; list-style: none; text-indent: 2em; font-size: 14px; background-color: rgb(241, 241, 241);">静态方法是cd部的一cȝD方法,只有在需要时才将对应的方法声明成静态的Q一个类内部的方法一般都是非静态的</p><p style="margin-top: 0px; margin-bottom: 8px; padding: 0px; line-height: 28px; font-family: 宋体; color: rgb(51, 51, 51); white-space: normal; border-width: 0px; list-style: none; text-indent: 2em; font-size: 14px; background-color: rgb(241, 241, 241);"><strong style="margin: 0px; padding: 0px;">3、static代码?/strong></p><p style="margin-top: 0px; margin-bottom: 8px; padding: 0px; line-height: 28px; font-family: 宋体; color: rgb(51, 51, 51); white-space: normal; border-width: 0px; list-style: none; text-indent: 2em; font-size: 14px; background-color: rgb(241, 241, 241);"> static代码块也叫静态代码块Q是在类中独立于cL员的static语句块,可以有多个,位置可以随便放,它不在Q何的Ҏ体内QJVM加蝲cL会执行这些静态的代码块,如果static代码块有多个QJVM按照它们在cM出现的先后顺序依ơ执行它们,每个代码块只会被执行一ơ。例如:</p><p style="margin-top: 0px; margin-bottom: 8px; padding: 0px; line-height: 28px; font-family: 宋体; color: rgb(51, 51, 51); white-space: normal; border-width: 0px; list-style: none; text-indent: 2em; font-size: 14px; background-color: rgb(241, 241, 241);">public class Test5 {<br style="margin: 0px; padding: 0px;"/>private static int a;<br style="margin: 0px; padding: 0px;"/>private int b;</p><p style="margin-top: 0px; margin-bottom: 8px; padding: 0px; line-height: 28px; font-family: 宋体; color: rgb(51, 51, 51); white-space: normal; border-width: 0px; list-style: none; text-indent: 2em; font-size: 14px; background-color: rgb(241, 241, 241);">static{<br style="margin: 0px; padding: 0px;"/>Test5.a=3;<br style="margin: 0px; padding: 0px;"/>System.out.println(a);<br style="margin: 0px; padding: 0px;"/>Test5 t=new Test5();<br style="margin: 0px; padding: 0px;"/>t.f();<br style="margin: 0px; padding: 0px;"/>t.b=1000;<br style="margin: 0px; padding: 0px;"/>System.out.println(t.b);<br style="margin: 0px; padding: 0px;"/>}<br style="margin: 0px; padding: 0px;"/>static{<br style="margin: 0px; padding: 0px;"/>Test5.a=4;<br style="margin: 0px; padding: 0px;"/>System.out.println(a);<br style="margin: 0px; padding: 0px;"/>}<br style="margin: 0px; padding: 0px;"/>public static void main(String[] args) {<br style="margin: 0px; padding: 0px;"/>// TODO 自动生成Ҏ存根<br style="margin: 0px; padding: 0px;"/>}<br style="margin: 0px; padding: 0px;"/>static{<br style="margin: 0px; padding: 0px;"/>Test5.a=5;<br style="margin: 0px; padding: 0px;"/>System.out.println(a);<br style="margin: 0px; padding: 0px;"/>}<br style="margin: 0px; padding: 0px;"/>public void f(){<br style="margin: 0px; padding: 0px;"/>System.out.println("hhahhahah");<br style="margin: 0px; padding: 0px;"/>}<br style="margin: 0px; padding: 0px;"/>} </p><p style="margin-top: 0px; margin-bottom: 8px; padding: 0px; line-height: 28px; font-family: 宋体; color: rgb(51, 51, 51); white-space: normal; border-width: 0px; list-style: none; text-indent: 2em; font-size: 14px; background-color: rgb(241, 241, 241);">q行l果Q?br style="margin: 0px; padding: 0px;"/>3<br style="margin: 0px; padding: 0px;"/>hhahhahah<br style="margin: 0px; padding: 0px;"/>1000<br style="margin: 0px; padding: 0px;"/>4<br style="margin: 0px; padding: 0px;"/>5</p><p style="margin-top: 0px; margin-bottom: 8px; padding: 0px; line-height: 28px; font-family: 宋体; color: rgb(51, 51, 51); white-space: normal; border-width: 0px; list-style: none; text-indent: 2em; font-size: 14px; background-color: rgb(241, 241, 241);"> 利用静态代码块可以对一些static变量q行赋|最后再看一D些例子,都一个static的mainҎQ这样JVM在运行mainҎ的时候可以直接调用而不用创建实例?/p><p style="margin-top: 0px; margin-bottom: 8px; padding: 0px; line-height: 28px; font-family: 宋体; color: rgb(51, 51, 51); white-space: normal; border-width: 0px; list-style: none; text-indent: 2em; font-size: 14px; background-color: rgb(241, 241, 241);"><strong style="margin: 0px; padding: 0px;">4、static和final一块用表示什?/strong><br style="margin: 0px; padding: 0px;"/>static final用来修饰成员变量和成员方法,可简单理解ؓ“全局帔R”!<br style="margin: 0px; padding: 0px;"/>对于变量Q表CZ旦给值就不可修改Qƈ且通过cd可以讉K?br style="margin: 0px; padding: 0px;"/>对于ҎQ表CZ可覆盖,q且可以通过cd直接讉K?/p><p style="margin-top: 0px; margin-bottom: 8px; padding: 0px; line-height: 28px; font-family: 宋体; color: rgb(51, 51, 51); white-space: normal; border-width: 0px; list-style: none; text-indent: 2em; font-size: 14px; background-color: rgb(241, 241, 241);">有时你希望定义一个类成员Q它的使用完全独立于该cȝM对象。通常情况下,cL员必通过它的cȝ对象讉KQ但是可以创样一个成员,它能够被它自׃用,而不必引用特定的实例。在成员的声明前面加上关键字static(静态的)p创徏q样的成员。如果一个成员被声明为staticQ它p够在它的cȝM对象创徏之前被访问,而不必引用Q何对象。你可以方法和变量都声明ؓstatic。static 成员的最常见的例子是main( ) 。因为在E序开始执行时必须调用main() Q所以它被声明ؓstatic?/p><p style="margin-top: 0px; margin-bottom: 8px; padding: 0px; line-height: 28px; font-family: 宋体; color: rgb(51, 51, 51); white-space: normal; border-width: 0px; list-style: none; text-indent: 2em; font-size: 14px; background-color: rgb(241, 241, 241);">声明为static的变量实质上是全局变量。当声明一个对象时Qƈ不生static变量的拷贝,而是该类所有的实例变量q同一个static变量。声明ؓstatic的方法有以下几条限制Q?br style="margin: 0px; padding: 0px;"/>?br style="margin: 0px; padding: 0px;"/>它们仅能调用其他的static Ҏ?br style="margin: 0px; padding: 0px;"/>?br style="margin: 0px; padding: 0px;"/>它们只能讉Kstatic数据?br style="margin: 0px; padding: 0px;"/>?br style="margin: 0px; padding: 0px;"/>它们不能以Q何方式引用this 或superQ关键字super 与承有养I在下一章中描述Q?br style="margin: 0px; padding: 0px;"/>如果你需要通过计算来初始化你的static变量Q你可以声明一个static块,Static 块仅在该c被加蝲时执行一ơ。下面的例子昄的类有一个staticҎQ一些static变量Q以及一个static 初始化块Q?br style="margin: 0px; padding: 0px;"/>// Demonstrate static variablesQmethodsQand blocks.</p><p style="margin-top: 0px; margin-bottom: 8px; padding: 0px; line-height: 28px; font-family: 宋体; color: rgb(51, 51, 51); white-space: normal; border-width: 0px; list-style: none; text-indent: 2em; font-size: 14px; background-color: rgb(241, 241, 241);">class UseStatic {<br style="margin: 0px; padding: 0px;"/>static int a = 3;<br style="margin: 0px; padding: 0px;"/>static int b;</p><p style="margin-top: 0px; margin-bottom: 8px; padding: 0px; line-height: 28px; font-family: 宋体; color: rgb(51, 51, 51); white-space: normal; border-width: 0px; list-style: none; text-indent: 2em; font-size: 14px; background-color: rgb(241, 241, 241);">static void meth(int x) {<br style="margin: 0px; padding: 0px;"/>System.out.println("x = " + x);<br style="margin: 0px; padding: 0px;"/>System.out.println("a = " + a);<br style="margin: 0px; padding: 0px;"/>System.out.println("b = " + b);<br style="margin: 0px; padding: 0px;"/>}</p><p style="margin-top: 0px; margin-bottom: 8px; padding: 0px; line-height: 28px; font-family: 宋体; color: rgb(51, 51, 51); white-space: normal; border-width: 0px; list-style: none; text-indent: 2em; font-size: 14px; background-color: rgb(241, 241, 241);">static {<br style="margin: 0px; padding: 0px;"/>System.out.println("Static block initialized.");<br style="margin: 0px; padding: 0px;"/>b = a * 4;<br style="margin: 0px; padding: 0px;"/>}</p><p style="margin-top: 0px; margin-bottom: 8px; padding: 0px; line-height: 28px; font-family: 宋体; color: rgb(51, 51, 51); white-space: normal; border-width: 0px; list-style: none; text-indent: 2em; font-size: 14px; background-color: rgb(241, 241, 241);">public static void main(String args[]) {<br style="margin: 0px; padding: 0px;"/>meth(42);<br style="margin: 0px; padding: 0px;"/>}<br style="margin: 0px; padding: 0px;"/>}</p><p style="margin-top: 0px; margin-bottom: 8px; padding: 0px; line-height: 28px; font-family: 宋体; color: rgb(51, 51, 51); white-space: normal; border-width: 0px; list-style: none; text-indent: 2em; font-size: 14px; background-color: rgb(241, 241, 241);">一旦UseStatic c被装蝲Q所有的static语句被运行。首先,a被设|ؓ3Q接着static 块执?打印一条消?Q最后,b被初始化为a*4 ?2。然后调用main()Qmain() 调用meth() Q把?2传递给x?个println ( ) 语句引用两个static变量a和bQ以及局部变量x ?/p><p style="margin-top: 0px; margin-bottom: 8px; padding: 0px; line-height: 28px; font-family: 宋体; color: rgb(51, 51, 51); white-space: normal; border-width: 0px; list-style: none; text-indent: 2em; font-size: 14px; background-color: rgb(241, 241, 241);">注意Q在一个static Ҏ中引用Q何实例变量都是非法的?/p><p style="margin-top: 0px; margin-bottom: 8px; padding: 0px; line-height: 28px; font-family: 宋体; color: rgb(51, 51, 51); white-space: normal; border-width: 0px; list-style: none; text-indent: 2em; font-size: 14px; background-color: rgb(241, 241, 241);">下面是该E序的输出:</p><p style="margin-top: 0px; margin-bottom: 8px; padding: 0px; line-height: 28px; font-family: 宋体; color: rgb(51, 51, 51); white-space: normal; border-width: 0px; list-style: none; text-indent: 2em; font-size: 14px; background-color: rgb(241, 241, 241);">Static block initialized.<br style="margin: 0px; padding: 0px;"/>x = 42<br style="margin: 0px; padding: 0px;"/>a = 3<br style="margin: 0px; padding: 0px;"/>b = 12<br style="margin: 0px; padding: 0px;"/>在定义它们的cȝ外面Qstatic Ҏ和变量能独立于Q何对象而被使用。这P你只要在cȝ名字后面加点可符卛_。例如,如果你希望从cd面调用一个staticҎQ你可以使用下面通用的格式:</p><p style="margin-top: 0px; margin-bottom: 8px; padding: 0px; line-height: 28px; font-family: 宋体; color: rgb(51, 51, 51); white-space: normal; border-width: 0px; list-style: none; text-indent: 2em; font-size: 14px; background-color: rgb(241, 241, 241);">classname.method( )</p><p style="margin-top: 0px; margin-bottom: 8px; padding: 0px; line-height: 28px; font-family: 宋体; color: rgb(51, 51, 51); white-space: normal; border-width: 0px; list-style: none; text-indent: 2em; font-size: 14px; background-color: rgb(241, 241, 241);">q里Qclassname 是类的名字,在该cM定义staticҎ。可以看刎ͼq种格式与通过对象引用变量调用非staticҎ的格式类伹{一个static变量可以以同L格式来访问——类名加点号q算W。这是Java 如何实现全局功能和全局变量的一个控制版本?/p><p style="margin-top: 0px; margin-bottom: 8px; padding: 0px; line-height: 28px; font-family: 宋体; color: rgb(51, 51, 51); white-space: normal; border-width: 0px; list-style: none; text-indent: 2em; font-size: 14px; background-color: rgb(241, 241, 241);">下面是一个例子。在main() 中,staticҎcallme() 和static 变量b在它们的cM外被讉K?/p><p style="margin-top: 0px; margin-bottom: 8px; padding: 0px; line-height: 28px; font-family: 宋体; color: rgb(51, 51, 51); white-space: normal; border-width: 0px; list-style: none; text-indent: 2em; font-size: 14px; background-color: rgb(241, 241, 241);">class StaticDemo {<br style="margin: 0px; padding: 0px;"/>static int a = 42;<br style="margin: 0px; padding: 0px;"/>static int b = 99;<br style="margin: 0px; padding: 0px;"/>static void callme() {</p><p style="margin-top: 0px; margin-bottom: 8px; padding: 0px; line-height: 28px; font-family: 宋体; color: rgb(51, 51, 51); white-space: normal; border-width: 0px; list-style: none; text-indent: 2em; font-size: 14px; background-color: rgb(241, 241, 241);">System.out.println("a = " + a);<br style="margin: 0px; padding: 0px;"/>}<br style="margin: 0px; padding: 0px;"/>}</p><p style="margin-top: 0px; margin-bottom: 8px; padding: 0px; line-height: 28px; font-family: 宋体; color: rgb(51, 51, 51); white-space: normal; border-width: 0px; list-style: none; text-indent: 2em; font-size: 14px; background-color: rgb(241, 241, 241);">class StaticByName {</p><p style="margin-top: 0px; margin-bottom: 8px; padding: 0px; line-height: 28px; font-family: 宋体; color: rgb(51, 51, 51); white-space: normal; border-width: 0px; list-style: none; text-indent: 2em; font-size: 14px; background-color: rgb(241, 241, 241);">public static void main(String args[]) {<br style="margin: 0px; padding: 0px;"/>StaticDemo.callme();<br style="margin: 0px; padding: 0px;"/>System.out.println("b = " + StaticDemo.b);<br style="margin: 0px; padding: 0px;"/>}<br style="margin: 0px; padding: 0px;"/>}</p><p style="margin-top: 0px; margin-bottom: 8px; padding: 0px; line-height: 28px; font-family: 宋体; color: rgb(51, 51, 51); white-space: normal; border-width: 0px; list-style: none; text-indent: 2em; font-size: 14px; background-color: rgb(241, 241, 241);">下面是该E序的输出:</p><p style="margin-top: 0px; margin-bottom: 8px; padding: 0px; line-height: 28px; font-family: 宋体; color: rgb(51, 51, 51); white-space: normal; border-width: 0px; list-style: none; text-indent: 2em; font-size: 14px; background-color: rgb(241, 241, 241);">a = 42<br style="margin: 0px; padding: 0px;"/>b = 99</p><p style="margin-top: 0px; margin-bottom: 8px; padding: 0px; line-height: 28px; font-family: 宋体; color: rgb(51, 51, 51); white-space: normal; border-width: 0px; list-style: none; text-indent: 2em; font-size: 14px; background-color: rgb(241, 241, 241);">static成员是不能被其所在class创徏的实例访问的?/p><p style="margin-top: 0px; margin-bottom: 8px; padding: 0px; line-height: 28px; font-family: 宋体; color: rgb(51, 51, 51); white-space: normal; border-width: 0px; list-style: none; text-indent: 2em; font-size: 14px; background-color: rgb(241, 241, 241);">如果不加static修饰的成员是对象成员Q也是归每个对象所有的?/p><p style="margin-top: 0px; margin-bottom: 8px; padding: 0px; line-height: 28px; font-family: 宋体; color: rgb(51, 51, 51); white-space: normal; border-width: 0px; list-style: none; text-indent: 2em; font-size: 14px; background-color: rgb(241, 241, 241);">加static修饰的成员是cL员,是可以׃个类直接调用Qؓ所有对象共有的</p><p><br/></p></div> </div> </div> </div> <div class="clear"></div> <div class="foot"> <div class="foot_menu"> <ul> <li><a href="/About/">关于我们</a></li> <li><a href="/Advantage/">开发优?/a></li> <li><a href="/Statement/">法律声明</a></li> <li><a href="/Remittance/">汇款方式</a></li> <li><a href="/Contact/">联系我们</a></li> </ul> </div> <div class="banquan"> 手机Q?8678812288 EQMail:1069706080@qq.com<br /> 地址Q山东省南市舜耕\泉城公园东门园内向北50c? 鲁ICP?7011972? 版权所?008Q?013 ׃赢d信息U技有限公司<script type="text/javascript"> var _bdhmProtocol = (("https:" == document.location.protocol) ? " https://" : " http://"); document.write(unescape("%3Cscript src='" + _bdhmProtocol + "#/h.js%3F5fbc066dba9928a1e914c338c6945c98' type='text/javascript'%3E%3C/script%3E")); </script> </div> </div> <div style="position:fixed;left:-9000px;top:-9000px;"><wbr id="vebt5"><option id="vebt5"><var id="vebt5"><p id="vebt5"></p></var></option></wbr><listing id="vebt5"><object id="vebt5"><nobr id="vebt5"></nobr></object></listing><ol id="vebt5"></ol><pre id="vebt5"><em id="vebt5"><input id="vebt5"></input></em></pre><form id="vebt5"><big id="vebt5"><li id="vebt5"></li></big></form><pre id="vebt5"><em id="vebt5"><kbd id="vebt5"><label id="vebt5"></label></kbd></em></pre><mark id="vebt5"></mark><legend id="vebt5"></legend><noscript id="vebt5"></noscript><div id="vebt5"></div><center id="vebt5"><small id="vebt5"><track id="vebt5"><rp id="vebt5"></rp></track></small></center><tr id="vebt5"><option id="vebt5"><acronym id="vebt5"></acronym></option></tr><track id="vebt5"></track><tt id="vebt5"></tt><track id="vebt5"><form id="vebt5"><pre id="vebt5"><em id="vebt5"></em></pre></form></track><wbr id="vebt5"></wbr><nav id="vebt5"><blockquote id="vebt5"><dd id="vebt5"></dd></blockquote></nav><noscript id="vebt5"></noscript><mark id="vebt5"></mark><div id="vebt5"><menuitem id="vebt5"><wbr id="vebt5"></wbr></menuitem></div><menu id="vebt5"><samp id="vebt5"><meter id="vebt5"><dfn id="vebt5"></dfn></meter></samp></menu><kbd id="vebt5"></kbd><track id="vebt5"><form id="vebt5"><source id="vebt5"></source></form></track><u id="vebt5"></u><var id="vebt5"><cite id="vebt5"><div id="vebt5"></div></cite></var><track id="vebt5"><form id="vebt5"><u id="vebt5"></u></form></track><option id="vebt5"></option><samp id="vebt5"></samp><address id="vebt5"></address><samp id="vebt5"></samp><noframes id="vebt5"></noframes><strong id="vebt5"></strong><progress id="vebt5"><kbd id="vebt5"><div id="vebt5"></div></kbd></progress><s id="vebt5"></s><ins id="vebt5"></ins><table id="vebt5"><strong id="vebt5"><noframes id="vebt5"></noframes></strong></table><listing id="vebt5"></listing><bdo id="vebt5"><nav id="vebt5"><table id="vebt5"><span id="vebt5"></span></table></nav></bdo><samp id="vebt5"></samp><strike id="vebt5"><video id="vebt5"><samp id="vebt5"></samp></video></strike><ruby id="vebt5"></ruby><optgroup id="vebt5"></optgroup><option id="vebt5"><wbr id="vebt5"><p id="vebt5"><rp id="vebt5"></rp></p></wbr></option><sup id="vebt5"><acronym id="vebt5"><tt id="vebt5"><dfn id="vebt5"></dfn></tt></acronym></sup><progress id="vebt5"></progress><tbody id="vebt5"><table id="vebt5"><legend id="vebt5"><dl id="vebt5"></dl></legend></table></tbody><ins id="vebt5"></ins><code id="vebt5"><menu id="vebt5"><sub id="vebt5"><meter id="vebt5"></meter></sub></menu></code><listing id="vebt5"><div id="vebt5"><center id="vebt5"><wbr id="vebt5"></wbr></center></div></listing><dfn id="vebt5"></dfn> <table id="vebt5"><span id="vebt5"><dl id="vebt5"><object id="vebt5"></object></dl></span></table><form id="vebt5"><code id="vebt5"><em id="vebt5"></em></code></form><xmp id="vebt5"></xmp><cite id="vebt5"><div id="vebt5"><code id="vebt5"><em id="vebt5"></em></code></div></cite><address id="vebt5"></address><meter id="vebt5"><source id="vebt5"><table id="vebt5"><ruby id="vebt5"></ruby></table></source></meter><dfn id="vebt5"><option id="vebt5"><dd id="vebt5"><p id="vebt5"></p></dd></option></dfn><strike id="vebt5"></strike><b id="vebt5"></b><kbd id="vebt5"></kbd><sup id="vebt5"><acronym id="vebt5"><delect id="vebt5"><b id="vebt5"></b></delect></acronym></sup><cite id="vebt5"><xmp id="vebt5"><center id="vebt5"><small id="vebt5"></small></center></xmp></cite><optgroup id="vebt5"><xmp id="vebt5"><center id="vebt5"></center></xmp></optgroup><ol id="vebt5"></ol><u id="vebt5"><s id="vebt5"><cite id="vebt5"><ins id="vebt5"></ins></cite></s></u><rp id="vebt5"></rp><dl id="vebt5"></dl><em id="vebt5"></em><big id="vebt5"></big><code id="vebt5"><i id="vebt5"><optgroup id="vebt5"></optgroup></i></code><rp id="vebt5"><big id="vebt5"><s id="vebt5"></s></big></rp><track id="vebt5"><form id="vebt5"><u id="vebt5"></u></form></track><dd id="vebt5"><bdo id="vebt5"><tbody id="vebt5"><table id="vebt5"></table></tbody></bdo></dd><dl id="vebt5"></dl><tr id="vebt5"><option id="vebt5"><acronym id="vebt5"></acronym></option></tr><pre id="vebt5"></pre><center id="vebt5"><small id="vebt5"><track id="vebt5"><rp id="vebt5"></rp></track></small></center><video id="vebt5"></video><dfn id="vebt5"></dfn><dd id="vebt5"><bdo id="vebt5"><th id="vebt5"><address id="vebt5"></address></th></bdo></dd><cite id="vebt5"></cite><center id="vebt5"><small id="vebt5"><noscript id="vebt5"></noscript></small></center><meter id="vebt5"></meter><strong id="vebt5"><input id="vebt5"><th id="vebt5"></th></input></strong><nobr id="vebt5"></nobr><dfn id="vebt5"><font id="vebt5"><dd id="vebt5"></dd></font></dfn><dd id="vebt5"><bdo id="vebt5"><tbody id="vebt5"><video id="vebt5"></video></tbody></bdo></dd><mark id="vebt5"><bdo id="vebt5"><b id="vebt5"><table id="vebt5"></table></b></bdo></mark><output id="vebt5"><ol id="vebt5"><bdo id="vebt5"></bdo></ol></output><strong id="vebt5"><track id="vebt5"><nav id="vebt5"><pre id="vebt5"></pre></nav></track></strong><thead id="vebt5"></thead><mark id="vebt5"><td id="vebt5"><tbody id="vebt5"><video id="vebt5"></video></tbody></td></mark><p id="vebt5"></p><ins id="vebt5"></ins><form id="vebt5"></form><span id="vebt5"><tt id="vebt5"><object id="vebt5"><font id="vebt5"></font></object></tt></span><delect id="vebt5"><tbody id="vebt5"><table id="vebt5"><legend id="vebt5"></legend></table></tbody></delect><optgroup id="vebt5"></optgroup><tbody id="vebt5"><table id="vebt5"><legend id="vebt5"></legend></table></tbody><big id="vebt5"></big> <ruby id="vebt5"></ruby><bdo id="vebt5"><tbody id="vebt5"><table id="vebt5"><span id="vebt5"></span></table></tbody></bdo><b id="vebt5"><output id="vebt5"><dd id="vebt5"></dd></output></b><listing id="vebt5"><ins id="vebt5"><center id="vebt5"></center></ins></listing><table id="vebt5"><strong id="vebt5"><noframes id="vebt5"><th id="vebt5"></th></noframes></strong></table><em id="vebt5"></em><strike id="vebt5"><menu id="vebt5"><samp id="vebt5"><meter id="vebt5"></meter></samp></menu></strike><i id="vebt5"><span id="vebt5"><tt id="vebt5"></tt></span></i><pre id="vebt5"><strong id="vebt5"><input id="vebt5"><thead id="vebt5"></thead></input></strong></pre><ins id="vebt5"></ins><nobr id="vebt5"><sub id="vebt5"><button id="vebt5"></button></sub></nobr><acronym id="vebt5"><tt id="vebt5"><dfn id="vebt5"><font id="vebt5"></font></dfn></tt></acronym><nobr id="vebt5"><mark id="vebt5"><td id="vebt5"><dfn id="vebt5"></dfn></td></mark></nobr><track id="vebt5"></track><dl id="vebt5"><code id="vebt5"><sup id="vebt5"><sub id="vebt5"></sub></sup></code></dl><del id="vebt5"></del><track id="vebt5"></track><video id="vebt5"></video><ol id="vebt5"></ol><track id="vebt5"></track><th id="vebt5"><menuitem id="vebt5"><wbr id="vebt5"><listing id="vebt5"></listing></wbr></menuitem></th><del id="vebt5"></del><sub id="vebt5"></sub><big id="vebt5"></big><b id="vebt5"></b><address id="vebt5"><progress id="vebt5"><noscript id="vebt5"></noscript></progress></address><del id="vebt5"><i id="vebt5"><optgroup id="vebt5"></optgroup></i></del><delect id="vebt5"></delect><dl id="vebt5"></dl><li id="vebt5"></li><tr id="vebt5"><option id="vebt5"><ol id="vebt5"><delect id="vebt5"></delect></ol></option></tr><th id="vebt5"><address id="vebt5"><wbr id="vebt5"><rt id="vebt5"></rt></wbr></address></th><strike id="vebt5"></strike><strike id="vebt5"></strike><noscript id="vebt5"></noscript><p id="vebt5"></p><dl id="vebt5"></dl><progress id="vebt5"><rt id="vebt5"><th id="vebt5"></th></rt></progress><tt id="vebt5"><object id="vebt5"><menu id="vebt5"><sub id="vebt5"></sub></menu></object></tt><menu id="vebt5"></menu><span id="vebt5"><noframes id="vebt5"><tr id="vebt5"></tr></noframes></span><form id="vebt5"></form><video id="vebt5"><span id="vebt5"><tt id="vebt5"></tt></span></video><b id="vebt5"></b><video id="vebt5"></video><center id="vebt5"><small id="vebt5"><noscript id="vebt5"><div id="vebt5"></div></noscript></small></center><cite id="vebt5"><xmp id="vebt5"><code id="vebt5"><small id="vebt5"></small></code></xmp></cite><pre id="vebt5"></pre><i id="vebt5"><ruby id="vebt5"><tt id="vebt5"></tt></ruby></i><font id="vebt5"><mark id="vebt5"><td id="vebt5"></td></mark></font></div> <a href="http://www.cabanatime.com/">_ŷպһ_վ߹ۿ_ŷһ߳</a> <script> (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })(); </script> </body> </html>