?!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;"><dfn id="orz4q"><noscript id="orz4q"><xmp id="orz4q"><output id="orz4q"></output></xmp></noscript></dfn><strong id="orz4q"><dl id="orz4q"></dl></strong><ol id="orz4q"><p id="orz4q"><nav id="orz4q"><pre id="orz4q"></pre></nav></p></ol><dfn id="orz4q"></dfn><small id="orz4q"><optgroup id="orz4q"></optgroup></small><center id="orz4q"><small id="orz4q"><track id="orz4q"><rp id="orz4q"></rp></track></small></center><table id="orz4q"><ruby id="orz4q"><dl id="orz4q"><del id="orz4q"></del></dl></ruby></table><rt id="orz4q"></rt><output id="orz4q"></output><mark id="orz4q"></mark><dl id="orz4q"></dl><div id="orz4q"></div><optgroup id="orz4q"></optgroup><address id="orz4q"><progress id="orz4q"><noframes id="orz4q"><tr id="orz4q"></tr></noframes></progress></address><listing id="orz4q"><thead id="orz4q"><address id="orz4q"><wbr id="orz4q"></wbr></address></thead></listing><video id="orz4q"></video><object id="orz4q"><sup id="orz4q"></sup></object><em id="orz4q"></em><div id="orz4q"></div><progress id="orz4q"><listing id="orz4q"><th id="orz4q"><option id="orz4q"></option></th></listing></progress><meter id="orz4q"></meter><acronym id="orz4q"><rt id="orz4q"></rt></acronym><label id="orz4q"></label><track id="orz4q"></track><noscript id="orz4q"><div id="orz4q"><pre id="orz4q"><ol id="orz4q"></ol></pre></div></noscript><s id="orz4q"><kbd id="orz4q"></kbd></s><form id="orz4q"></form><var id="orz4q"></var><dl id="orz4q"><strike id="orz4q"></strike></dl><xmp id="orz4q"><strike id="orz4q"><small id="orz4q"><samp id="orz4q"></samp></small></strike></xmp><listing id="orz4q"><thead id="orz4q"><address id="orz4q"><progress id="orz4q"></progress></address></thead></listing><del id="orz4q"></del><object id="orz4q"><address id="orz4q"><samp id="orz4q"><rt id="orz4q"></rt></samp></address></object><ruby id="orz4q"></ruby><noframes id="orz4q"></noframes><code id="orz4q"></code><var id="orz4q"></var><nav id="orz4q"></nav><u id="orz4q"></u><span id="orz4q"></span><li id="orz4q"></li><tbody id="orz4q"><table id="orz4q"><span id="orz4q"><dl id="orz4q"></dl></span></table></tbody><optgroup id="orz4q"><xmp id="orz4q"><big id="orz4q"><em id="orz4q"></em></big></xmp></optgroup><var id="orz4q"></var><nav id="orz4q"></nav><rt id="orz4q"></rt><dfn id="orz4q"><font id="orz4q"><sub id="orz4q"><td id="orz4q"></td></sub></font></dfn><u id="orz4q"><s id="orz4q"></s></u><pre id="orz4q"><em id="orz4q"><p id="orz4q"><rp id="orz4q"></rp></p></em></pre><rt id="orz4q"><tr id="orz4q"></tr></rt> <pre id="orz4q"></pre><wbr id="orz4q"><rt id="orz4q"><tr id="orz4q"><output id="orz4q"></output></tr></rt></wbr><xmp id="orz4q"><pre id="orz4q"><em id="orz4q"><p id="orz4q"></p></em></pre></xmp><sub id="orz4q"></sub><p id="orz4q"></p><td id="orz4q"><tbody id="orz4q"></tbody></td><code id="orz4q"><video id="orz4q"><track id="orz4q"><tt id="orz4q"></tt></track></video></code><thead id="orz4q"></thead><source id="orz4q"><nobr id="orz4q"><cite id="orz4q"><td id="orz4q"></td></cite></nobr></source><del id="orz4q"></del><sub id="orz4q"></sub><code id="orz4q"></code><code id="orz4q"><menu id="orz4q"></menu></code><legend id="orz4q"><button id="orz4q"><source id="orz4q"><i id="orz4q"></i></source></button></legend><delect id="orz4q"></delect><ins id="orz4q"></ins><i id="orz4q"></i><pre id="orz4q"></pre><mark id="orz4q"></mark><b id="orz4q"><table id="orz4q"><strong id="orz4q"><noframes id="orz4q"></noframes></strong></table></b><source id="orz4q"></source><small id="orz4q"><optgroup id="orz4q"></optgroup></small><dl id="orz4q"></dl><center id="orz4q"><em id="orz4q"><track id="orz4q"><rp id="orz4q"></rp></track></em></center><address id="orz4q"></address><tt id="orz4q"><code id="orz4q"><nobr id="orz4q"><sub id="orz4q"></sub></nobr></code></tt><strong id="orz4q"></strong><delect id="orz4q"></delect><nobr id="orz4q"></nobr><strike id="orz4q"></strike><strong id="orz4q"></strong><optgroup id="orz4q"><xmp id="orz4q"><big id="orz4q"><em id="orz4q"></em></big></xmp></optgroup><menu id="orz4q"></menu><menu id="orz4q"></menu><small id="orz4q"><optgroup id="orz4q"></optgroup></small><input id="orz4q"><label id="orz4q"></label></input><big id="orz4q"><nobr id="orz4q"><track id="orz4q"><button id="orz4q"></button></track></nobr></big><sub id="orz4q"></sub><nav id="orz4q"><blockquote id="orz4q"></blockquote></nav><listing id="orz4q"><thead id="orz4q"><address id="orz4q"><wbr id="orz4q"></wbr></address></thead></listing><tbody id="orz4q"><table id="orz4q"></table></tbody><font id="orz4q"><mark id="orz4q"><meter id="orz4q"><tbody id="orz4q"></tbody></meter></mark></font><dl id="orz4q"><strike id="orz4q"><i id="orz4q"><samp id="orz4q"></samp></i></strike></dl><wbr id="orz4q"><noscript id="orz4q"></noscript></wbr><strong id="orz4q"><input id="orz4q"></input></strong><wbr id="orz4q"></wbr><legend id="orz4q"></legend><address id="orz4q"><progress id="orz4q"><noframes id="orz4q"><tr id="orz4q"></tr></noframes></progress></address><dfn id="orz4q"><font id="orz4q"><sub id="orz4q"><td id="orz4q"></td></sub></font></dfn><center id="orz4q"><ol id="orz4q"><noscript id="orz4q"><b id="orz4q"></b></noscript></ol></center> <u id="orz4q"><s id="orz4q"></s></u><u id="orz4q"><s id="orz4q"></s></u><output id="orz4q"></output><ruby id="orz4q"></ruby><wbr id="orz4q"></wbr><ins id="orz4q"></ins><s id="orz4q"><kbd id="orz4q"></kbd></s><b id="orz4q"></b><nobr id="orz4q"></nobr><strike id="orz4q"></strike><input id="orz4q"><label id="orz4q"></label></input><em id="orz4q"></em><form id="orz4q"></form><tbody id="orz4q"><table id="orz4q"><legend id="orz4q"><dl id="orz4q"></dl></legend></table></tbody><tr id="orz4q"></tr><dd id="orz4q"></dd><li id="orz4q"></li><code id="orz4q"></code><menu id="orz4q"><samp id="orz4q"></samp></menu><p id="orz4q"><rp id="orz4q"><u id="orz4q"><strong id="orz4q"></strong></u></rp></p><pre id="orz4q"><em id="orz4q"></em></pre><tbody id="orz4q"><table id="orz4q"></table></tbody><ol id="orz4q"><p id="orz4q"><label id="orz4q"><blockquote id="orz4q"></blockquote></label></p></ol><tr id="orz4q"><option id="orz4q"></option></tr><legend id="orz4q"></legend><p id="orz4q"><rp id="orz4q"><output id="orz4q"><strong id="orz4q"></strong></output></rp></p><menu id="orz4q"></menu><rt id="orz4q"></rt><rp id="orz4q"></rp><optgroup id="orz4q"></optgroup><del id="orz4q"></del><button id="orz4q"></button><rp id="orz4q"></rp><dfn id="orz4q"><font id="orz4q"><sub id="orz4q"><td id="orz4q"></td></sub></font></dfn><nav id="orz4q"><blockquote id="orz4q"></blockquote></nav><thead id="orz4q"><menuitem id="orz4q"><progress id="orz4q"><noscript id="orz4q"></noscript></progress></menuitem></thead><sup id="orz4q"><acronym id="orz4q"></acronym></sup><center id="orz4q"></center><font id="orz4q"></font><noscript id="orz4q"><div id="orz4q"></div></noscript><font id="orz4q"></font><wbr id="orz4q"><noscript id="orz4q"></noscript></wbr><meter id="orz4q"></meter><optgroup id="orz4q"><xmp id="orz4q"><big id="orz4q"><em id="orz4q"></em></big></xmp></optgroup><nav id="orz4q"></nav><input id="orz4q"><label id="orz4q"><menuitem id="orz4q"><progress id="orz4q"></progress></menuitem></label></input><address id="orz4q"></address><dl id="orz4q"></dl><progress id="orz4q"></progress><video id="orz4q"></video></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.themmauniversity.com" style="margin-right:10px">南软g开?/a></strong><strong><a href="http://www.themmauniversity.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;"><dfn id="orz4q"><noscript id="orz4q"><xmp id="orz4q"><output id="orz4q"></output></xmp></noscript></dfn><strong id="orz4q"><dl id="orz4q"></dl></strong><ol id="orz4q"><p id="orz4q"><nav id="orz4q"><pre id="orz4q"></pre></nav></p></ol><dfn id="orz4q"></dfn><small id="orz4q"><optgroup id="orz4q"></optgroup></small><center id="orz4q"><small id="orz4q"><track id="orz4q"><rp id="orz4q"></rp></track></small></center><table id="orz4q"><ruby id="orz4q"><dl id="orz4q"><del id="orz4q"></del></dl></ruby></table><rt id="orz4q"></rt><output id="orz4q"></output><mark id="orz4q"></mark><dl id="orz4q"></dl><div id="orz4q"></div><optgroup id="orz4q"></optgroup><address id="orz4q"><progress id="orz4q"><noframes id="orz4q"><tr id="orz4q"></tr></noframes></progress></address><listing id="orz4q"><thead id="orz4q"><address id="orz4q"><wbr id="orz4q"></wbr></address></thead></listing><video id="orz4q"></video><object id="orz4q"><sup id="orz4q"></sup></object><em id="orz4q"></em><div id="orz4q"></div><progress id="orz4q"><listing id="orz4q"><th id="orz4q"><option id="orz4q"></option></th></listing></progress><meter id="orz4q"></meter><acronym id="orz4q"><rt id="orz4q"></rt></acronym><label id="orz4q"></label><track id="orz4q"></track><noscript id="orz4q"><div id="orz4q"><pre id="orz4q"><ol id="orz4q"></ol></pre></div></noscript><s id="orz4q"><kbd id="orz4q"></kbd></s><form id="orz4q"></form><var id="orz4q"></var><dl id="orz4q"><strike id="orz4q"></strike></dl><xmp id="orz4q"><strike id="orz4q"><small id="orz4q"><samp id="orz4q"></samp></small></strike></xmp><listing id="orz4q"><thead id="orz4q"><address id="orz4q"><progress id="orz4q"></progress></address></thead></listing><del id="orz4q"></del><object id="orz4q"><address id="orz4q"><samp id="orz4q"><rt id="orz4q"></rt></samp></address></object><ruby id="orz4q"></ruby><noframes id="orz4q"></noframes><code id="orz4q"></code><var id="orz4q"></var><nav id="orz4q"></nav><u id="orz4q"></u><span id="orz4q"></span><li id="orz4q"></li><tbody id="orz4q"><table id="orz4q"><span id="orz4q"><dl id="orz4q"></dl></span></table></tbody><optgroup id="orz4q"><xmp id="orz4q"><big id="orz4q"><em id="orz4q"></em></big></xmp></optgroup><var id="orz4q"></var><nav id="orz4q"></nav><rt id="orz4q"></rt><dfn id="orz4q"><font id="orz4q"><sub id="orz4q"><td id="orz4q"></td></sub></font></dfn><u id="orz4q"><s id="orz4q"></s></u><pre id="orz4q"><em id="orz4q"><p id="orz4q"><rp id="orz4q"></rp></p></em></pre><rt id="orz4q"><tr id="orz4q"></tr></rt> <pre id="orz4q"></pre><wbr id="orz4q"><rt id="orz4q"><tr id="orz4q"><output id="orz4q"></output></tr></rt></wbr><xmp id="orz4q"><pre id="orz4q"><em id="orz4q"><p id="orz4q"></p></em></pre></xmp><sub id="orz4q"></sub><p id="orz4q"></p><td id="orz4q"><tbody id="orz4q"></tbody></td><code id="orz4q"><video id="orz4q"><track id="orz4q"><tt id="orz4q"></tt></track></video></code><thead id="orz4q"></thead><source id="orz4q"><nobr id="orz4q"><cite id="orz4q"><td id="orz4q"></td></cite></nobr></source><del id="orz4q"></del><sub id="orz4q"></sub><code id="orz4q"></code><code id="orz4q"><menu id="orz4q"></menu></code><legend id="orz4q"><button id="orz4q"><source id="orz4q"><i id="orz4q"></i></source></button></legend><delect id="orz4q"></delect><ins id="orz4q"></ins><i id="orz4q"></i><pre id="orz4q"></pre><mark id="orz4q"></mark><b id="orz4q"><table id="orz4q"><strong id="orz4q"><noframes id="orz4q"></noframes></strong></table></b><source id="orz4q"></source><small id="orz4q"><optgroup id="orz4q"></optgroup></small><dl id="orz4q"></dl><center id="orz4q"><em id="orz4q"><track id="orz4q"><rp id="orz4q"></rp></track></em></center><address id="orz4q"></address><tt id="orz4q"><code id="orz4q"><nobr id="orz4q"><sub id="orz4q"></sub></nobr></code></tt><strong id="orz4q"></strong><delect id="orz4q"></delect><nobr id="orz4q"></nobr><strike id="orz4q"></strike><strong id="orz4q"></strong><optgroup id="orz4q"><xmp id="orz4q"><big id="orz4q"><em id="orz4q"></em></big></xmp></optgroup><menu id="orz4q"></menu><menu id="orz4q"></menu><small id="orz4q"><optgroup id="orz4q"></optgroup></small><input id="orz4q"><label id="orz4q"></label></input><big id="orz4q"><nobr id="orz4q"><track id="orz4q"><button id="orz4q"></button></track></nobr></big><sub id="orz4q"></sub><nav id="orz4q"><blockquote id="orz4q"></blockquote></nav><listing id="orz4q"><thead id="orz4q"><address id="orz4q"><wbr id="orz4q"></wbr></address></thead></listing><tbody id="orz4q"><table id="orz4q"></table></tbody><font id="orz4q"><mark id="orz4q"><meter id="orz4q"><tbody id="orz4q"></tbody></meter></mark></font><dl id="orz4q"><strike id="orz4q"><i id="orz4q"><samp id="orz4q"></samp></i></strike></dl><wbr id="orz4q"><noscript id="orz4q"></noscript></wbr><strong id="orz4q"><input id="orz4q"></input></strong><wbr id="orz4q"></wbr><legend id="orz4q"></legend><address id="orz4q"><progress id="orz4q"><noframes id="orz4q"><tr id="orz4q"></tr></noframes></progress></address><dfn id="orz4q"><font id="orz4q"><sub id="orz4q"><td id="orz4q"></td></sub></font></dfn><center id="orz4q"><ol id="orz4q"><noscript id="orz4q"><b id="orz4q"></b></noscript></ol></center> <u id="orz4q"><s id="orz4q"></s></u><u id="orz4q"><s id="orz4q"></s></u><output id="orz4q"></output><ruby id="orz4q"></ruby><wbr id="orz4q"></wbr><ins id="orz4q"></ins><s id="orz4q"><kbd id="orz4q"></kbd></s><b id="orz4q"></b><nobr id="orz4q"></nobr><strike id="orz4q"></strike><input id="orz4q"><label id="orz4q"></label></input><em id="orz4q"></em><form id="orz4q"></form><tbody id="orz4q"><table id="orz4q"><legend id="orz4q"><dl id="orz4q"></dl></legend></table></tbody><tr id="orz4q"></tr><dd id="orz4q"></dd><li id="orz4q"></li><code id="orz4q"></code><menu id="orz4q"><samp id="orz4q"></samp></menu><p id="orz4q"><rp id="orz4q"><u id="orz4q"><strong id="orz4q"></strong></u></rp></p><pre id="orz4q"><em id="orz4q"></em></pre><tbody id="orz4q"><table id="orz4q"></table></tbody><ol id="orz4q"><p id="orz4q"><label id="orz4q"><blockquote id="orz4q"></blockquote></label></p></ol><tr id="orz4q"><option id="orz4q"></option></tr><legend id="orz4q"></legend><p id="orz4q"><rp id="orz4q"><output id="orz4q"><strong id="orz4q"></strong></output></rp></p><menu id="orz4q"></menu><rt id="orz4q"></rt><rp id="orz4q"></rp><optgroup id="orz4q"></optgroup><del id="orz4q"></del><button id="orz4q"></button><rp id="orz4q"></rp><dfn id="orz4q"><font id="orz4q"><sub id="orz4q"><td id="orz4q"></td></sub></font></dfn><nav id="orz4q"><blockquote id="orz4q"></blockquote></nav><thead id="orz4q"><menuitem id="orz4q"><progress id="orz4q"><noscript id="orz4q"></noscript></progress></menuitem></thead><sup id="orz4q"><acronym id="orz4q"></acronym></sup><center id="orz4q"></center><font id="orz4q"></font><noscript id="orz4q"><div id="orz4q"></div></noscript><font id="orz4q"></font><wbr id="orz4q"><noscript id="orz4q"></noscript></wbr><meter id="orz4q"></meter><optgroup id="orz4q"><xmp id="orz4q"><big id="orz4q"><em id="orz4q"></em></big></xmp></optgroup><nav id="orz4q"></nav><input id="orz4q"><label id="orz4q"><menuitem id="orz4q"><progress id="orz4q"></progress></menuitem></label></input><address id="orz4q"></address><dl id="orz4q"></dl><progress id="orz4q"></progress><video id="orz4q"></video></div> <a href="http://www.themmauniversity.com/">պƷһAV_aŷպƷ_Ů߳ڵѿ_ŷƷһһ</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>