<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
  <channel>
    <title>lw223</title>
    <description>新人新气象</description>
    <link>http://lw223.javaeye.com</link>
    <language>UTF-8</language>
    <copyright>Copyright 2003-2008, JavaEye.com</copyright>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <generator>JavaEye - 做最棒的软件开发交流社区</generator>
          <item>
        <title>把家安在这里</title>
        <author>lw223</author>
        <description>
          <![CDATA[
          <br/>
          作者: <a href="http://lw223.javaeye.com">lw223</a>&nbsp;
                    链接：<a href="http://lw223.javaeye.com/blog/211480" style="color:red;">http://lw223.javaeye.com/blog/211480</a>&nbsp;
          发表时间: 2008年07月04日
          <br/><br/>
          声明：本文系JavaEye网站发布的原创博客文章，未经作者书面许可，严禁任何网站转载本文，否则必将追究法律责任！
          <br/><br/>
          <p>csdn的blog越改版越让人失望，我决定把家先搬到javaeye上面。</p>
<p>来这里因为有给csdn的blog搬家的功能<img src="../../images/smiles/icon_lol.gif" alt="" />，速度也快。</p>
          <br/><br/>
          <span style="color:red;">
            <a href="http://lw223.javaeye.com/blog/211480#comments" style="color:red;">已有 <strong>0</strong> 人发表留言，猛击-&gt;&gt;<strong>这里</strong>&lt;&lt;-参与讨论</a>
          </span>
          <br/><br/><br/>
          <span style="color:#E28822;">JavaEye推荐</span>
          <br/>
          <ul class='adverts'><li><a href='/adverts/42' target='_blank'><span style="color:red;font-weight:bold;">搜狐网站诚聘Java、PHP和C++工程师</span></a></li><li><a href='/adverts/41' target='_blank'><span style="color:red;font-weight:bold;">Windows7在微软WinHEC 2008上揭开神秘面纱</span></a></li></ul>
          <br/><br/><br/>
          ]]>
        </description>
        <pubDate>Fri, 04 Jul 2008 12:55:30 +0800</pubDate>
        <link>http://lw223.javaeye.com/blog/211480</link>
        <guid>http://lw223.javaeye.com/blog/211480</guid>
      </item>
          <item>
        <title>csdn的blog很让我失望</title>
        <author>lw223</author>
        <description>
          <![CDATA[
          <br/>
          作者: <a href="http://lw223.javaeye.com">lw223</a>&nbsp;
                    链接：<a href="http://lw223.javaeye.com/blog/211435" style="color:red;">http://lw223.javaeye.com/blog/211435</a>&nbsp;
          发表时间: 2008年07月04日
          <br/><br/>
          声明：本文系JavaEye网站发布的原创博客文章，未经作者书面许可，严禁任何网站转载本文，否则必将追究法律责任！
          <br/><br/>
          <p>&nbsp;&nbsp;&nbsp;&nbsp; blog功能真的配不上csdn这个名字啊。虽然注册了，但是很少来发帖子。主要就是blog功能太难用了，bug多多，功能弱弱。</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp; 在这里发帖子的人大部分都不会在乎花里胡哨的功能，只要简洁，快速，能粘贴代码就够了。</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp; blog的功能csdn要是做不好就外包出去吧。csdn上这么多高手呢。</p>
          <br/><br/>
          <span style="color:red;">
            <a href="http://lw223.javaeye.com/blog/211435#comments" style="color:red;">已有 <strong>0</strong> 人发表留言，猛击-&gt;&gt;<strong>这里</strong>&lt;&lt;-参与讨论</a>
          </span>
          <br/><br/><br/>
          <span style="color:#E28822;">JavaEye推荐</span>
          <br/>
          <ul class='adverts'><li><a href='/adverts/41' target='_blank'><span style="color:red;font-weight:bold;">Windows7在微软WinHEC 2008上揭开神秘面纱</span></a></li><li><a href='/adverts/42' target='_blank'><span style="color:red;font-weight:bold;">搜狐网站诚聘Java、PHP和C++工程师</span></a></li></ul>
          <br/><br/><br/>
          ]]>
        </description>
        <pubDate>Fri, 04 Jul 2008 11:16:53 +0800</pubDate>
        <link>http://lw223.javaeye.com/blog/211435</link>
        <guid>http://lw223.javaeye.com/blog/211435</guid>
      </item>
          <item>
        <title>seam和oracle数据库</title>
        <author>lw223</author>
        <description>
          <![CDATA[
          <br/>
          作者: <a href="http://lw223.javaeye.com">lw223</a>&nbsp;
                    链接：<a href="http://lw223.javaeye.com/blog/211437" style="color:red;">http://lw223.javaeye.com/blog/211437</a>&nbsp;
          发表时间: 2008年05月13日
          <br/><br/>
          声明：本文系JavaEye网站发布的原创博客文章，未经作者书面许可，严禁任何网站转载本文，否则必将追究法律责任！
          <br/><br/>
          <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 前几周学习了一下seam框架，seam把ejb3和JSF整合的很好，开发也简便了很多，很像ROR^_^。seam开发可以将更多的精力放在业务需求上，减少消耗在代码上的时间。<br />&nbsp;&nbsp; &nbsp; 为了方便seam开发，jboss官方的jboss tools支持seam，可以用它生成一个seam projcet。由于插件自身待的是mysql的JDBC驱动（这个免费^_^），<br />所以导致在用oracle或者是其他数据库时会有点麻烦。<br />&nbsp;&nbsp; &nbsp; 应用其它数据库其实很简单，下面就以oracle数据库来举例说明一下吧<br />&nbsp;&nbsp;&nbsp;&nbsp; 首先将eclipse的视图换成seam，然后新建一个seam project，名字为seamdemo。工程建好以后（用jboss做服务器会产生三个工程），将oracle的jdbc驱动包（ojdbc14_g9.jar）复制到&ldquo;...\jboss-4.2.0.GA\server\default\lib&rdquo;目录下面<br />&nbsp;在工程里找到seamdemo-ds.xml这个文件，然后将它复制到&ldquo;...\jboss-4.2.0.GA\server\default\deploy&rdquo;目录下面，完成后就可以启动jboss服务器运行你的seam工程啦。（"...\"是你机器的jboss路径）<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 不爽的地方是seam和jboss结合的很好，但是放到tomcat下开发就会很麻烦，要修改tomcat配置，还有我用jboss tools建工程时，工程名字不能有&ldquo;-&rdquo;，例如&ldquo;seam-demo&rdquo;这个名字再用jboss tools建工程时，&ldquo;-&rdquo;就是非法字符，工程就会建立失败。</p>
          <br/><br/>
          <span style="color:red;">
            <a href="http://lw223.javaeye.com/blog/211437#comments" style="color:red;">已有 <strong>8</strong> 人发表留言，猛击-&gt;&gt;<strong>这里</strong>&lt;&lt;-参与讨论</a>
          </span>
          <br/><br/><br/>
          <span style="color:#E28822;">JavaEye推荐</span>
          <br/>
          <ul class='adverts'><li><a href='/adverts/42' target='_blank'><span style="color:red;font-weight:bold;">搜狐网站诚聘Java、PHP和C++工程师</span></a></li><li><a href='/adverts/41' target='_blank'><span style="color:red;font-weight:bold;">Windows7在微软WinHEC 2008上揭开神秘面纱</span></a></li></ul>
          <br/><br/><br/>
          ]]>
        </description>
        <pubDate>Tue, 13 May 2008 15:07:16 +0800</pubDate>
        <link>http://lw223.javaeye.com/blog/211437</link>
        <guid>http://lw223.javaeye.com/blog/211437</guid>
      </item>
          <item>
        <title>随机生成验证码的例子</title>
        <author>lw223</author>
        <description>
          <![CDATA[
          <br/>
          作者: <a href="http://lw223.javaeye.com">lw223</a>&nbsp;
                    链接：<a href="http://lw223.javaeye.com/blog/211438" style="color:red;">http://lw223.javaeye.com/blog/211438</a>&nbsp;
          发表时间: 2008年05月13日
          <br/><br/>
          声明：本文系JavaEye网站发布的原创博客文章，未经作者书面许可，严禁任何网站转载本文，否则必将追究法律责任！
          <br/><br/>
          最近在读&ldquo;struts2权威指南&rdquo;这本书，里面例子中有段随机生成验证码的代码，转帖过来，以后没准能用到。<br />虽然现在现成的东西很多，不过还是要多学学代码，看看作者怎写的 :-)<br />
<div style="border: 0.5pt solid windowtext; padding: 4px 5.4pt; background: rgb(230, 230, 230) none repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; width: 95%;">
<div><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" alt="" /><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);">&nbsp;java.awt.Color;<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" alt="" /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);">&nbsp;java.awt.Font;<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" alt="" /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);">&nbsp;java.awt.Graphics;<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" alt="" /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);">&nbsp;java.awt.image.BufferedImage;<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" alt="" /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);">&nbsp;java.io.IOException;<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" alt="" /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);">&nbsp;java.util.Random;<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" alt="" /><br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" alt="" /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);">&nbsp;javax.imageio.ImageIO;<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" alt="" /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);">&nbsp;javax.servlet.ServletException;<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" alt="" /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);">&nbsp;javax.servlet.http.HttpServlet;<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" alt="" /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);">&nbsp;javax.servlet.http.HttpServletRequest;<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" alt="" /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);">&nbsp;javax.servlet.http.HttpServletResponse;<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" alt="" /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);">&nbsp;javax.servlet.http.HttpSession;<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" alt="" /><br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif" id="_444_3178_Open_Image" align="top" onclick="this.style.display='none'; document.getElementById('_444_3178_Open_Text').style.display='none'; document.getElementById('_444_3178_Closed_Image').style.display='inline'; document.getElementById('_444_3178_Closed_Text').style.display='inline';" alt="" /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif" id="_444_3178_Closed_Image" align="top" onclick="this.style.display='none'; document.getElementById('_444_3178_Closed_Text').style.display='none'; document.getElementById('_444_3178_Open_Image').style.display='inline'; document.getElementById('_444_3178_Open_Text').style.display='inline';" alt="" style="display: none;" /></span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);">&nbsp;AuthImg&nbsp;</span><span style="color: rgb(0, 0, 255);">extends</span><span style="color: rgb(0, 0, 0);">&nbsp;HttpServlet&nbsp;</span><span id="_444_3178_Closed_Text" style="border: 1px solid rgb(128, 128, 128); background-color: rgb(255, 255, 255); display: none;">...</span><span id="_444_3178_Open_Text"><span style="color: rgb(0, 0, 0);">{<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" /><br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">&nbsp;设置图形验证码中字符串的字体和大小</span><span style="color: rgb(0, 128, 0);"><br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" /></span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">private</span><span style="color: rgb(0, 0, 0);">&nbsp;Font&nbsp;mFont&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);">&nbsp;Font(</span><span style="color: rgb(0, 0, 0);">&quot;</span><span style="color: rgb(0, 0, 0);">Arial&nbsp;Black</span><span style="color: rgb(0, 0, 0);">&quot;</span><span style="color: rgb(0, 0, 0);">,&nbsp;Font.PLAIN,&nbsp;</span><span style="color: rgb(0, 0, 0);">16</span><span style="color: rgb(0, 0, 0);">);<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" /><br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif" id="_577_596_Open_Image" align="top" onclick="this.style.display='none'; document.getElementById('_577_596_Open_Text').style.display='none'; document.getElementById('_577_596_Closed_Image').style.display='inline'; document.getElementById('_577_596_Closed_Text').style.display='inline';" alt="" /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif" id="_577_596_Closed_Image" align="top" onclick="this.style.display='none'; document.getElementById('_577_596_Closed_Text').style.display='none'; document.getElementById('_577_596_Open_Image').style.display='inline'; document.getElementById('_577_596_Open_Text').style.display='inline';" alt="" style="display: none;" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);">&nbsp;init()&nbsp;</span><span style="color: rgb(0, 0, 255);">throws</span><span style="color: rgb(0, 0, 0);">&nbsp;ServletException&nbsp;</span><span id="_577_596_Closed_Text" style="border: 1px solid rgb(128, 128, 128); background-color: rgb(255, 255, 255); display: none;">...</span><span id="_577_596_Open_Text"><span style="color: rgb(0, 0, 0);">{<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">super</span><span style="color: rgb(0, 0, 0);">.init();<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: rgb(0, 0, 0);"><br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" /><br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">&nbsp;生成随即颜色</span><span style="color: rgb(0, 128, 0);"><br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif" id="_646_888_Open_Image" align="top" onclick="this.style.display='none'; document.getElementById('_646_888_Open_Text').style.display='none'; document.getElementById('_646_888_Closed_Image').style.display='inline'; document.getElementById('_646_888_Closed_Text').style.display='inline';" alt="" /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif" id="_646_888_Closed_Image" align="top" onclick="this.style.display='none'; document.getElementById('_646_888_Closed_Text').style.display='none'; document.getElementById('_646_888_Open_Image').style.display='inline'; document.getElementById('_646_888_Open_Text').style.display='inline';" alt="" style="display: none;" /></span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;Color&nbsp;getRandColor(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">&nbsp;fc,&nbsp;</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">&nbsp;bc)&nbsp;</span><span id="_646_888_Closed_Text" style="border: 1px solid rgb(128, 128, 128); background-color: rgb(255, 255, 255); display: none;">...</span><span id="_646_888_Open_Text"><span style="color: rgb(0, 0, 0);">{<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Random&nbsp;random&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);">&nbsp;Random();<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">&nbsp;(fc&nbsp;</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">255</span><span style="color: rgb(0, 0, 0);">)<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fc&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">255</span><span style="color: rgb(0, 0, 0);">;<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">&nbsp;(bc&nbsp;</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">255</span><span style="color: rgb(0, 0, 0);">)<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bc&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">255</span><span style="color: rgb(0, 0, 0);">;<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">&nbsp;r&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;fc&nbsp;</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">&nbsp;random.nextInt(bc&nbsp;</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">&nbsp;fc);<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">&nbsp;g&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;fc&nbsp;</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">&nbsp;random.nextInt(bc&nbsp;</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">&nbsp;fc);<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">&nbsp;b&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;fc&nbsp;</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">&nbsp;random.nextInt(bc&nbsp;</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">&nbsp;fc);<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);">&nbsp;Color(r,&nbsp;g,&nbsp;b);<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: rgb(0, 0, 0);"><br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" /><br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">&nbsp;生成服务器响应的service方法</span><span style="color: rgb(0, 128, 0);"><br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" /></span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);">&nbsp;service(HttpServletRequest&nbsp;request,&nbsp;HttpServletResponse&nbsp;response)<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif" id="_1032_2649_Open_Image" align="top" onclick="this.style.display='none'; document.getElementById('_1032_2649_Open_Text').style.display='none'; document.getElementById('_1032_2649_Closed_Image').style.display='inline'; document.getElementById('_1032_2649_Closed_Text').style.display='inline';" alt="" /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif" id="_1032_2649_Closed_Image" align="top" onclick="this.style.display='none'; document.getElementById('_1032_2649_Closed_Text').style.display='none'; document.getElementById('_1032_2649_Open_Image').style.display='inline'; document.getElementById('_1032_2649_Open_Text').style.display='inline';" alt="" style="display: none;" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">throws</span><span style="color: rgb(0, 0, 0);">&nbsp;ServletException,&nbsp;IOException&nbsp;</span><span id="_1032_2649_Closed_Text" style="border: 1px solid rgb(128, 128, 128); background-color: rgb(255, 255, 255); display: none;">...</span><span id="_1032_2649_Open_Text"><span style="color: rgb(0, 0, 0);">{<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">&nbsp;组织生成页面内容被缓存，保证每次重新生成随机验证码</span><span style="color: rgb(0, 128, 0);"><br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" /></span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;response.setHeader(</span><span style="color: rgb(0, 0, 0);">&quot;</span><span style="color: rgb(0, 0, 0);">Pragma</span><span style="color: rgb(0, 0, 0);">&quot;</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">&quot;</span><span style="color: rgb(0, 0, 0);">No-cache</span><span style="color: rgb(0, 0, 0);">&quot;</span><span style="color: rgb(0, 0, 0);">);<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;response.setHeader(</span><span style="color: rgb(0, 0, 0);">&quot;</span><span style="color: rgb(0, 0, 0);">Cache-Control</span><span style="color: rgb(0, 0, 0);">&quot;</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">&quot;</span><span style="color: rgb(0, 0, 0);">no-cache</span><span style="color: rgb(0, 0, 0);">&quot;</span><span style="color: rgb(0, 0, 0);">);<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;response.setDateHeader(</span><span style="color: rgb(0, 0, 0);">&quot;</span><span style="color: rgb(0, 0, 0);">Expires</span><span style="color: rgb(0, 0, 0);">&quot;</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">);<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">&nbsp;指定&nbsp;图形验证码大小</span><span style="color: rgb(0, 128, 0);"><br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" /></span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">&nbsp;width&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">100</span><span style="color: rgb(0, 0, 0);">,&nbsp;height&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">8</span><span style="color: rgb(0, 0, 0);">;<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">&nbsp;生成一张新图片</span><span style="color: rgb(0, 128, 0);"><br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" /></span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BufferedImage&nbsp;image&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);">&nbsp;BufferedImage(width,&nbsp;height,<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BufferedImage.TYPE_INT_RGB);<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">&nbsp;在图片中绘制内容</span><span style="color: rgb(0, 128, 0);"><br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" /></span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Graphics&nbsp;g&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;image.getGraphics();<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Random&nbsp;random&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);">&nbsp;Random();<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;g.setColor(getRandColor(</span><span style="color: rgb(0, 0, 0);">200</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">250</span><span style="color: rgb(0, 0, 0);">));<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;g.fillRect(</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">,&nbsp;width&nbsp;</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">,&nbsp;height&nbsp;</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">);<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;g.setColor(</span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);">&nbsp;Color(</span><span style="color: rgb(0, 0, 0);">102</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">102</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">102</span><span style="color: rgb(0, 0, 0);">));<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;g.setFont(mFont);<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">&nbsp;随即生成线条，让图片看起来更加杂乱</span><span style="color: rgb(0, 128, 0);"><br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" /></span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;g.setColor(getRandColor(</span><span style="color: rgb(0, 0, 0);">160</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">200</span><span style="color: rgb(0, 0, 0);">));<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif" id="_1666_1851_Open_Image" align="top" onclick="this.style.display='none'; document.getElementById('_1666_1851_Open_Text').style.display='none'; document.getElementById('_1666_1851_Closed_Image').style.display='inline'; document.getElementById('_1666_1851_Closed_Text').style.display='inline';" alt="" /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif" id="_1666_1851_Closed_Image" align="top" onclick="this.style.display='none'; document.getElementById('_1666_1851_Closed_Text').style.display='none'; document.getElementById('_1666_1851_Open_Image').style.display='inline'; document.getElementById('_1666_1851_Open_Text').style.display='inline';" alt="" style="display: none;" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">&nbsp;(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">&nbsp;i&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;&nbsp;i&nbsp;</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">155</span><span style="color: rgb(0, 0, 0);">;&nbsp;i</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">)&nbsp;</span><span id="_1666_1851_Closed_Text" style="border: 1px solid rgb(128, 128, 128); background-color: rgb(255, 255, 255); display: none;">...</span><span id="_1666_1851_Open_Text"><span style="color: rgb(0, 0, 0);">{<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">&nbsp;x1&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;random.nextInt(width&nbsp;</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">);<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">&nbsp;y1&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;random.nextInt(height&nbsp;</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">);<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">&nbsp;x2&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;random.nextInt(</span><span style="color: rgb(0, 0, 0);">6</span><span style="color: rgb(0, 0, 0);">)&nbsp;</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">;<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">&nbsp;y2&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;random.nextInt(</span><span style="color: rgb(0, 0, 0);">12</span><span style="color: rgb(0, 0, 0);">)&nbsp;</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">;<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;g.drawLine(x1,&nbsp;y1,&nbsp;x2,&nbsp;y2);<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: rgb(0, 0, 0);"><br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">&nbsp;随机生成线条，让图片看起来更杂乱</span><span style="color: rgb(0, 128, 0);"><br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif" id="_1906_2091_Open_Image" align="top" onclick="this.style.display='none'; document.getElementById('_1906_2091_Open_Text').style.display='none'; document.getElementById('_1906_2091_Closed_Image').style.display='inline'; document.getElementById('_1906_2091_Closed_Text').style.display='inline';" alt="" /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif" id="_1906_2091_Closed_Image" align="top" onclick="this.style.display='none'; document.getElementById('_1906_2091_Closed_Text').style.display='none'; document.getElementById('_1906_2091_Open_Image').style.display='inline'; document.getElementById('_1906_2091_Open_Text').style.display='inline';" alt="" style="display: none;" /></span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">&nbsp;(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">&nbsp;i&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;&nbsp;i&nbsp;</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">70</span><span style="color: rgb(0, 0, 0);">;&nbsp;i</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">)&nbsp;</span><span id="_1906_2091_Closed_Text" style="border: 1px solid rgb(128, 128, 128); background-color: rgb(255, 255, 255); display: none;">...</span><span id="_1906_2091_Open_Text"><span style="color: rgb(0, 0, 0);">{<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">&nbsp;x1&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;random.nextInt(width&nbsp;</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">);<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">&nbsp;y1&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;random.nextInt(height&nbsp;</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">);<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">&nbsp;x2&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;random.nextInt(</span><span style="color: rgb(0, 0, 0);">6</span><span style="color: rgb(0, 0, 0);">)&nbsp;</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">;<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">&nbsp;y2&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;random.nextInt(</span><span style="color: rgb(0, 0, 0);">12</span><span style="color: rgb(0, 0, 0);">)&nbsp;</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">;<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;g.drawLine(x1,&nbsp;y1,&nbsp;x2,&nbsp;y2);<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: rgb(0, 0, 0);"><br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">&nbsp;读取变量，用于保存系统生成的随机字符串</span><span style="color: rgb(0, 128, 0);"><br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" /></span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;sRand&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">&quot;&quot;</span><span style="color: rgb(0, 0, 0);">;<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif" id="_2169_2418_Open_Image" align="top" onclick="this.style.display='none'; document.getElementById('_2169_2418_Open_Text').style.display='none'; document.getElementById('_2169_2418_Closed_Image').style.display='inline'; document.getElementById('_2169_2418_Closed_Text').style.display='inline';" alt="" /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif" id="_2169_2418_Closed_Image" align="top" onclick="this.style.display='none'; document.getElementById('_2169_2418_Closed_Text').style.display='none'; document.getElementById('_2169_2418_Open_Image').style.display='inline'; document.getElementById('_2169_2418_Open_Text').style.display='inline';" alt="" style="display: none;" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">&nbsp;(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">&nbsp;i&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;&nbsp;i&nbsp;</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">6</span><span style="color: rgb(0, 0, 0);">;&nbsp;i</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">)&nbsp;</span><span id="_2169_2418_Closed_Text" style="border: 1px solid rgb(128, 128, 128); background-color: rgb(255, 255, 255); display: none;">...</span><span id="_2169_2418_Open_Text"><span style="color: rgb(0, 0, 0);">{<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">&nbsp;取得一个随机生成的字符</span><span style="color: rgb(0, 128, 0);"><br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" /></span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;tmp&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;getRandomChar();<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sRand&nbsp;</span><span style="color: rgb(0, 0, 0);">+=</span><span style="color: rgb(0, 0, 0);">&nbsp;tmp;<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">&nbsp;将系统随机生成的字符添加到图形验证码图片上</span><span style="color: rgb(0, 128, 0);"><br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" /></span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;g.setColor(</span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);">&nbsp;Color(</span><span style="color: rgb(0, 0, 0);">20</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">&nbsp;random.nextInt(</span><span style="color: rgb(0, 0, 0);">110</span><span style="color: rgb(0, 0, 0);">),&nbsp;</span><span style="color: rgb(0, 0, 0);">20</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">&nbsp;random<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.nextInt(</span><span style="color: rgb(0, 0, 0);">110</span><span style="color: rgb(0, 0, 0);">),&nbsp;</span><span style="color: rgb(0, 0, 0);">20</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">&nbsp;random.nextInt(</span><span style="color: rgb(0, 0, 0);">110</span><span style="color: rgb(0, 0, 0);">)));<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;g.drawString(tmp,&nbsp;</span><span style="color: rgb(0, 0, 0);">15</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">&nbsp;i&nbsp;</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">10</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">15</span><span style="color: rgb(0, 0, 0);">);<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: rgb(0, 0, 0);"><br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">&nbsp;取得用户Session</span><span style="color: rgb(0, 128, 0);"><br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" /></span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HttpSession&nbsp;session&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;request.getSession(</span><span style="color: rgb(0, 0, 255);">true</span><span style="color: rgb(0, 0, 0);">);<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">&nbsp;将系统随机生成的图形验证码添加到用户Session中</span><span style="color: rgb(0, 128, 0);"><br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" /></span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;session.setAttribute(</span><span style="color: rgb(0, 0, 0);">&quot;</span><span style="color: rgb(0, 0, 0);">rand</span><span style="color: rgb(0, 0, 0);">&quot;</span><span style="color: rgb(0, 0, 0);">,&nbsp;sRand);<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;g.dispose();<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">&nbsp;输出图形验证码图片</span><span style="color: rgb(0, 128, 0);"><br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" /></span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ImageIO.write(image,&nbsp;</span><span style="color: rgb(0, 0, 0);">&quot;</span><span style="color: rgb(0, 0, 0);">JPEG</span><span style="color: rgb(0, 0, 0);">&quot;</span><span style="color: rgb(0, 0, 0);">,&nbsp;response.getOutputStream());<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: rgb(0, 0, 0);"><br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" /><br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif" id="_2684_3176_Open_Image" align="top" onclick="this.style.display='none'; document.getElementById('_2684_3176_Open_Text').style.display='none'; document.getElementById('_2684_3176_Closed_Image').style.display='inline'; document.getElementById('_2684_3176_Closed_Text').style.display='inline';" alt="" /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif" id="_2684_3176_Closed_Image" align="top" onclick="this.style.display='none'; document.getElementById('_2684_3176_Closed_Text').style.display='none'; document.getElementById('_2684_3176_Open_Image').style.display='inline'; document.getElementById('_2684_3176_Open_Text').style.display='inline';" alt="" style="display: none;" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">private</span><span style="color: rgb(0, 0, 0);">&nbsp;String&nbsp;getRandomChar()&nbsp;</span><span id="_2684_3176_Closed_Text" style="border: 1px solid rgb(128, 128, 128); background-color: rgb(255, 255, 255); display: none;">...</span><span id="_2684_3176_Open_Text"><span style="color: rgb(0, 0, 0);">{<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">&nbsp;rand&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">)&nbsp;Math.round(Math.random()&nbsp;</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">);<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">long</span><span style="color: rgb(0, 0, 0);">&nbsp;itmp&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);">&nbsp;ctmp&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">;<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">&nbsp;根据rand的值来决定是生成小写字母，大写字母和数字</span><span style="color: rgb(0, 128, 0);"><br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif" id="_2825_3173_Open_Image" align="top" onclick="this.style.display='none'; document.getElementById('_2825_3173_Open_Text').style.display='none'; document.getElementById('_2825_3173_Closed_Image').style.display='inline'; document.getElementById('_2825_3173_Closed_Text').style.display='inline';" alt="" /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif" id="_2825_3173_Closed_Image" align="top" onclick="this.style.display='none'; document.getElementById('_2825_3173_Closed_Text').style.display='none'; document.getElementById('_2825_3173_Open_Image').style.display='inline'; document.getElementById('_2825_3173_Open_Text').style.display='inline';" alt="" style="display: none;" /></span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">switch</span><span style="color: rgb(0, 0, 0);">&nbsp;(rand)&nbsp;</span><span id="_2825_3173_Closed_Text" style="border: 1px solid rgb(128, 128, 128); background-color: rgb(255, 255, 255); display: none;">...</span><span id="_2825_3173_Open_Text"><span style="color: rgb(0, 0, 0);">{<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">&nbsp;生成大写字母</span><span style="color: rgb(0, 128, 0);"><br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" /></span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">case</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">:<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;itmp&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;Math.round(Math.random()&nbsp;</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">25</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">65</span><span style="color: rgb(0, 0, 0);">);<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ctmp&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;(</span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);">)&nbsp;itmp;<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">&nbsp;String.valueOf(ctmp);<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">&nbsp;生成小写字母</span><span style="color: rgb(0, 128, 0);"><br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" /></span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">case</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">:<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;itmp&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;Math.round(Math.random()&nbsp;</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">25</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">97</span><span style="color: rgb(0, 0, 0);">);<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ctmp&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;(</span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);">)&nbsp;itmp;<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">&nbsp;String.valueOf(ctmp);<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">&nbsp;生成数字</span><span style="color: rgb(0, 128, 0);"><br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" /></span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">default</span><span style="color: rgb(0, 0, 0);">:<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;itmp&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;Math.round(Math.random()&nbsp;</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">9</span><span style="color: rgb(0, 0, 0);">);<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">&nbsp;String.valueOf(itmp);<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: rgb(0, 0, 0);"><br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: rgb(0, 0, 0);"><br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif" align="top" alt="" />}</span></span></div>
</div>
          <br/><br/>
          <span style="color:red;">
            <a href="http://lw223.javaeye.com/blog/211438#comments" style="color:red;">已有 <strong>0</strong> 人发表留言，猛击-&gt;&gt;<strong>这里</strong>&lt;&lt;-参与讨论</a>
          </span>
          <br/><br/><br/>
          <span style="color:#E28822;">JavaEye推荐</span>
          <br/>
          <ul class='adverts'><li><a href='/adverts/42' target='_blank'><span style="color:red;font-weight:bold;">搜狐网站诚聘Java、PHP和C++工程师</span></a></li><li><a href='/adverts/41' target='_blank'><span style="color:red;font-weight:bold;">Windows7在微软WinHEC 2008上揭开神秘面纱</span></a></li></ul>
          <br/><br/><br/>
          ]]>
        </description>
        <pubDate>Tue, 13 May 2008 14:01:32 +0800</pubDate>
        <link>http://lw223.javaeye.com/blog/211438</link>
        <guid>http://lw223.javaeye.com/blog/211438</guid>
      </item>
          <item>
        <title>如何删除detached instance？  </title>
        <author>lw223</author>
        <description>
          <![CDATA[
          <br/>
          作者: <a href="http://lw223.javaeye.com">lw223</a>&nbsp;
                    链接：<a href="http://lw223.javaeye.com/blog/211439" style="color:red;">http://lw223.javaeye.com/blog/211439</a>&nbsp;
          发表时间: 2008年04月15日
          <br/><br/>
          声明：本文系JavaEye网站发布的原创博客文章，未经作者书面许可，严禁任何网站转载本文，否则必将追究法律责任！
          <br/><br/>
          <p><a href="http://blog.csdn.net/MyJSF/archive/2007/12/01/1910491.aspx">http://blog.csdn.net/MyJSF/archive/2007/12/01/1910491.aspx</a></p>
<p>应用实例环境：Spring jpa hibernate3<br />常用数据库表的删除办法，一般都会在DAO类中提供delete.如下例：<br />public class UnitDAO implements IUnitDAO {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; private EntityManager entityManager;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; @PersistenceContext<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public void setEntityManager(EntityManager entityManager) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.entityManager = entityManager;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; private EntityManager getEntityManager() {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return this.entityManager;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public void delete(Unit persistentInstance) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; getEntityManager().remove(persistentInstance);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } catch (RuntimeException re) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; throw re;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public List&lt;Unit&gt; findAll() {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; String queryString = &quot;select model from Unit model&quot;;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return getEntityManager().createQuery(queryString).getResultList();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } catch (RuntimeException re) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; throw re;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />}<br />看上去，没有任何问题，这也是在MyEclipse中自动产生的代码。但是，在实际运行过程中，界面会调用findAll()获得全部的unit记录显示在界面层，根据业务需求，用户选择一个unit进行删除，当调用delete方法时，出现异常：<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; java.lang.IllegalArgumentException: Removing a detached instance com.gotop.rbac.model.Unit#1<br />&nbsp;&nbsp; 意思就是说，在删除一个detached instance出错。</p>
<ul>
    <li>&nbsp;&nbsp;&nbsp; 解决办法：</li>
</ul>
<p>看看Hibernate是如何处理对象的.Chapter 10. Working with objects. <a href="http://www.hibernate.org/hib_docs/reference/en/html/objectstate.html">http://www.hibernate.org/hib_docs/reference/en/html/objectstate.html</a><br />&nbsp;&nbsp;&nbsp; 说的很清楚。Hibernate object states有三种状态：Transient、Persistent、Detached。关于Detached，是这么说的：<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Detached - a detached instance is an object that has been persistent, but its Session has been closed. The reference to the object is still valid, of course, and the detached instance might even be modified in this state. A detached instance can be reattached to a new Session at a later point in time, making it (and all the modifications) persistent again. <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 一个detached instance是一个已经持久化的对象，但是它的Session已经关闭了，它的引用依然有效，当然，detached instance可能被修改。detached instance能够在以后可以重新附属到一个新的Session,使之能重新序列化。<br />&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;好了，找到解决办法了，在删除之前把这个Detached instance绑定到当前的Sesssion，在用当前Sesssion删除此instance。getEntityManager()提供merge方法实现。<br />修改后的delete代码：<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public void delete(Unit persistentInstance) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; getEntityManager().remove(getEntityManager().merge(persistentInstance));<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } catch (RuntimeException re) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; throw re;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 千万不要写成：<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; getEntityManager().merge(persistentInstance);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; getEntityManager().remove(persistentInstance);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;执行完merge后persistentInstance还是detached, merge后返回的新对象才是允许删除的。&nbsp;&nbsp;</p>
          <br/><br/>
          <span style="color:red;">
            <a href="http://lw223.javaeye.com/blog/211439#comments" style="color:red;">已有 <strong>0</strong> 人发表留言，猛击-&gt;&gt;<strong>这里</strong>&lt;&lt;-参与讨论</a>
          </span>
          <br/><br/><br/>
          <span style="color:#E28822;">JavaEye推荐</span>
          <br/>
          <ul class='adverts'><li><a href='/adverts/42' target='_blank'><span style="color:red;font-weight:bold;">搜狐网站诚聘Java、PHP和C++工程师</span></a></li><li><a href='/adverts/41' target='_blank'><span style="color:red;font-weight:bold;">Windows7在微软WinHEC 2008上揭开神秘面纱</span></a></li></ul>
          <br/><br/><br/>
          ]]>
        </description>
        <pubDate>Tue, 15 Apr 2008 16:57:00 +0800</pubDate>
        <link>http://lw223.javaeye.com/blog/211439</link>
        <guid>http://lw223.javaeye.com/blog/211439</guid>
      </item>
          <item>
        <title>Oracle to_date的使用方法</title>
        <author>lw223</author>
        <description>
          <![CDATA[
          <br/>
          作者: <a href="http://lw223.javaeye.com">lw223</a>&nbsp;
                    链接：<a href="http://lw223.javaeye.com/blog/211440" style="color:red;">http://lw223.javaeye.com/blog/211440</a>&nbsp;
          发表时间: 2008年03月24日
          <br/><br/>
          声明：本文系JavaEye网站发布的原创博客文章，未经作者书面许可，严禁任何网站转载本文，否则必将追究法律责任！
          <br/><br/>
          <p>日期格式参数 含义说明 <br />D 一周中的星期几 <br />DAY 天的名字，使用空格填充到9个字符 <br />DD 月中的第几天&nbsp;&nbsp; <br />DDD 年中的第几天 <br />DY 天的简写名 <br />IW ISO标准的年中的第几周 <br />IYYY ISO标准的四位年份 <br />YYYY 四位年份 <br />YYY,YY,Y 年份的最后三位，两位，一位 <br />HH 小时，按12小时计 <br />HH24 小时，按24小时计&nbsp;&nbsp; <br />MI 分&nbsp;&nbsp; <br />SS 秒&nbsp;&nbsp; <br />MM 月&nbsp;&nbsp; <br />Mon 月份的简写&nbsp;&nbsp; <br />Month 月份的全名&nbsp;&nbsp; <br />W 该月的第几个星期 <br />WW 年中的第几个星期</p>
<p>1.日期时间间隔操作&nbsp;&nbsp; <br />　　当前时间减去7分钟的时间<br />　　select sysdate,sysdate - interval &rsquo;7&rsquo; MINUTE from dual <br />　　当前时间减去7小时的时间 <br />　　select sysdate - interval &rsquo;7&rsquo; hour from dual&nbsp;&nbsp; <br />　　当前时间减去7天的时间&nbsp;&nbsp; <br />　　select sysdate - interval &rsquo;7&rsquo; day from dual <br />　　当前时间减去7月的时间&nbsp;&nbsp; <br />　　select sysdate,sysdate - interval &rsquo;7&rsquo; month from dual <br />　　当前时间减去7年的时间 <br />　　select sysdate,sysdate - interval &rsquo;7&rsquo; year from dual <br />　　时间间隔乘以一个数字&nbsp;&nbsp; <br />　　select sysdate,sysdate - 8 *interval &rsquo;2&rsquo; hour from dual <br />2.日期到字符操作 <br />　　select sysdate,to_char(sysdate,&rsquo;yyyy-mm-dd hh24:mi:ss&rsquo;) from dual&nbsp;&nbsp; <br />　　select sysdate,to_char(sysdate,&rsquo;yyyy-mm-dd hh:mi:ss&rsquo;) from dual&nbsp;&nbsp; <br />　　select sysdate,to_char(sysdate,&rsquo;yyyy-ddd hh:mi:ss&rsquo;) from dual <br />　　select sysdate,to_char(sysdate,&rsquo;yyyy-mm iw-d hh:mi:ss&rsquo;) from dual&nbsp;&nbsp; <br />　　参考oracle的相关关文档(ORACLE901DOC/SERVER.901/A90125/SQL_ELEMENTS4.HTM#48515)&nbsp;&nbsp; <br />3. 字符到日期操作&nbsp;&nbsp; <br />　　select to_date(&rsquo;2003-10-17 21:15:37&rsquo;,&rsquo;yyyy-mm-dd hh24:mi:ss&rsquo;) from dual&nbsp;&nbsp; <br />　　具体用法和上面的to_char差不多。 <br />4. trunk/ ROUND函数的使用 <br />　　select trunc(sysdate ,&rsquo;YEAR&rsquo;) from dual <br />　　select trunc(sysdate ) from dual <br />　　select to_char(trunc(sysdate ,&rsquo;YYYY&rsquo;),&rsquo;YYYY&rsquo;) from dual <br />5.oracle有毫秒级的数据类型 <br />　　--返回当前时间 年月日小时分秒毫秒 <br />　　select to_char(current_timestamp(5),&rsquo;DD-MON-YYYY HH24:MI:SSxFF&rsquo;) from dual; <br />　　--返回当前 时间的秒毫秒，可以指定秒后面的精度(最大=9)&nbsp;&nbsp; <br />　　select to_char(current_timestamp(9),&rsquo;MI:SSxFF&rsquo;) from dual; <br />6.计算程序运行的时间(ms) <br />　　declare <br />　　type rc is ref cursor;&nbsp;&nbsp; <br />　　l_rc rc; <br />　　l_dummy all_objects.object_name%type;&nbsp;&nbsp; <br />　　l_start number default dbms_utility.get_time; <br />　　begin <br />　　for I in 1 .. 1000 <br />　　loop <br />　　open l_rc for&nbsp;&nbsp; <br />　　&rsquo;select object_name from all_objects &rsquo;||&nbsp;&nbsp; <br />　　&rsquo;where object_id = &rsquo; || i;&nbsp;&nbsp; <br />　　fetch l_rc into l_dummy;&nbsp;&nbsp; <br />　　close l_rc; <br />　　end loop;&nbsp;&nbsp; <br />　　dbms_output.put_line <br />　　( round( (dbms_utility.get_time-l_start)/100, 2 ) || <br />　　&rsquo; seconds...&rsquo; ); <br />　　end;<br />&nbsp;</p>
<p>本文来自: 中国自学编程网(<a href="http://www.zxbc.cn/">www.zxbc.cn</a>) 详细出处参考：<a href="http://www.zxbc.cn/html/Oracle/1713175257949.html">http://www.zxbc.cn/html/Oracle/1713175257949.html</a></p>
          <br/><br/>
          <span style="color:red;">
            <a href="http://lw223.javaeye.com/blog/211440#comments" style="color:red;">已有 <strong>0</strong> 人发表留言，猛击-&gt;&gt;<strong>这里</strong>&lt;&lt;-参与讨论</a>
          </span>
          <br/><br/><br/>
          <span style="color:#E28822;">JavaEye推荐</span>
          <br/>
          <ul class='adverts'><li><a href='/adverts/42' target='_blank'><span style="color:red;font-weight:bold;">搜狐网站诚聘Java、PHP和C++工程师</span></a></li><li><a href='/adverts/41' target='_blank'><span style="color:red;font-weight:bold;">Windows7在微软WinHEC 2008上揭开神秘面纱</span></a></li></ul>
          <br/><br/><br/>
          ]]>
        </description>
        <pubDate>Mon, 24 Mar 2008 17:10:00 +0800</pubDate>
        <link>http://lw223.javaeye.com/blog/211440</link>
        <guid>http://lw223.javaeye.com/blog/211440</guid>
      </item>
          <item>
        <title>Java中static、this、super、final用法</title>
        <author>lw223</author>
        <description>
          <![CDATA[
          <br/>
          作者: <a href="http://lw223.javaeye.com">lw223</a>&nbsp;
                    链接：<a href="http://lw223.javaeye.com/blog/211441" style="color:red;">http://lw223.javaeye.com/blog/211441</a>&nbsp;
          发表时间: 2008年01月09日
          <br/><br/>
          声明：本文系JavaEye网站发布的原创博客文章，未经作者书面许可，严禁任何网站转载本文，否则必将追究法律责任！
          <br/><br/>
          <p>记录Java中static、this、super、final用法，方便自己以后查看&nbsp;</p>
<p>一、static</p>
<p>　　请先看下面这段程序：</p>
<div style="BORDER-RIGHT: windowtext 0.5pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 0.5pt solid; PADDING-LEFT: 5.4pt; BACKGROUND: #e6e6e6; PADDING-BOTTOM: 4px; BORDER-LEFT: windowtext 0.5pt solid; WIDTH: 95%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: windowtext 0.5pt solid">
<div><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif" id="_18_128_Open_Image" onclick="this.style.display='none'; document.getElementById('_18_128_Open_Text').style.display='none'; document.getElementById('_18_128_Closed_Image').style.display='inline'; document.getElementById('_18_128_Closed_Text').style.display='inline';" align="top" alt="" /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif" id="_18_128_Closed_Image" onclick="this.style.display='none'; document.getElementById('_18_128_Closed_Text').style.display='none'; document.getElementById('_18_128_Open_Image').style.display='inline'; document.getElementById('_18_128_Open_Text').style.display='inline';" align="top" alt="" style="DISPLAY: none" /><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">&nbsp;Hello</span><span id="_18_128_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">...</span><span id="_18_128_Open_Text"><span style="COLOR: #000000">{<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif" id="_62_124_Open_Image" onclick="this.style.display='none'; document.getElementById('_62_124_Open_Text').style.display='none'; document.getElementById('_62_124_Closed_Image').style.display='inline'; document.getElementById('_62_124_Closed_Text').style.display='inline';" align="top" alt="" /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif" id="_62_124_Closed_Image" onclick="this.style.display='none'; document.getElementById('_62_124_Closed_Text').style.display='none'; document.getElementById('_62_124_Open_Image').style.display='inline'; document.getElementById('_62_124_Open_Text').style.display='inline';" align="top" alt="" style="DISPLAY: none" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">static</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;main(String[]&nbsp;args)</span><span id="_62_124_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">...</span><span id="_62_124_Open_Text"><span style="COLOR: #000000">{&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">(1)</span><span style="COLOR: #008000"><br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" /></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(</span><span style="COLOR: #000000">&quot;</span><span style="COLOR: #000000">Hello,world!</span><span style="COLOR: #000000">&quot;</span><span style="COLOR: #000000">);&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">(2)</span><span style="COLOR: #008000"><br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" alt="" /></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif" align="top" alt="" />&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" alt="" /></span></div>
</div>
看过这段程序，对于大多数学过Java 的从来说，都不陌生。即使没有学过Java，而学过其它的高级语言，例如C，那你也应该能看懂这段代码的意思。它只是简单的输出&ldquo;Hello,world&rdquo;，一点别的用处都没有，然而，它却展示了static关键字的主要用法。
<p>　　在1处，我们定义了一个静态的方法名为main，这就意味着告诉Java编译器，我这个方法不需要创建一个此类的对象即可使用。你还得你是怎么运行这个程序吗？一般，我们都是在命令行下，打入如下的命令(加下划线为手动输入)：</p>
<p>javac Hello.java<br />java Hello<br />Hello,world!</p>
<p>　　这就是你运行的过程，第一行用来编译Hello.java这个文件，执行完后，如果你查看当前，会发现多了一个Hello.class文件，那就是第一行产生的Java二进制字节码。第二行就是执行一个Java程序的最普遍做法。执行结果如你所料。在2中，你可能会想，为什么要这样才能输出。好，我们来分解一下这条语句。（如果没有安装Java文档，请到Sun的官方网站浏览J2SE API）首先，System是位于java.lang包中的一个核心类，如果你查看它的定义，你会发现有这样一行：public static final PrintStream out;接着在进一步，点击PrintStream这个超链接，在METHOD页面，你会看到大量定义的方法，查找println，会有这样一行：</p>
<p>public void println(String x)。</p>
<p>　　好了，现在你应该明白为什么我们要那样调用了，out是System的一个静态变量，所以可以直接使用，而out所属的类有一个println方法。</p>
<p><strong>静态方法</strong></p>
<p>　　通常，在一个类中定义一个方法为static，那就是说，无需本类的对象即可调用此方法。如下所示：</p>
<div style="BORDER-RIGHT: windowtext 0.5pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 0.5pt solid; PADDING-LEFT: 5.4pt; BACKGROUND: #e6e6e6; PADDING-BOTTOM: 4px; BORDER-LEFT: windowtext 0.5pt solid; WIDTH: 95%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: windowtext 0.5pt solid">
<div><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif" id="_12_74_Open_Image" onclick="this.style.display='none'; document.getElementById('_12_74_Open_Text').style.display='none'; document.getElementById('_12_74_Closed_Image').style.display='inline'; document.getElementById('_12_74_Closed_Text').style.display='inline';" align="top" alt="" /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif" id="_12_74_Closed_Image" onclick="this.style.display='none'; document.getElementById('_12_74_Closed_Text').style.display='none'; document.getElementById('_12_74_Open_Image').style.display='inline'; document.getElementById('_12_74_Open_Text').style.display='inline';" align="top" alt="" style="DISPLAY: none" /><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">&nbsp;Simple</span><span id="_12_74_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">...</span><span id="_12_74_Open_Text"><span style="COLOR: #000000">{<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif" id="_33_72_Open_Image" onclick="this.style.display='none'; document.getElementById('_33_72_Open_Text').style.display='none'; document.getElementById('_33_72_Closed_Image').style.display='inline'; document.getElementById('_33_72_Closed_Text').style.display='inline';" align="top" alt="" /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif" id="_33_72_Closed_Image" onclick="this.style.display='none'; document.getElementById('_33_72_Closed_Text').style.display='none'; document.getElementById('_33_72_Open_Image').style.display='inline'; document.getElementById('_33_72_Open_Text').style.display='inline';" align="top" alt="" style="DISPLAY: none" />&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">static</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;go()</span><span id="_33_72_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">...</span><span id="_33_72_Open_Text"><span style="COLOR: #000000">{<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(</span><span style="COLOR: #000000">&quot;</span><span style="COLOR: #000000">Go...</span><span style="COLOR: #000000">&quot;</span><span style="COLOR: #000000">);<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif" align="top" alt="" />}</span></span><span style="COLOR: #000000"><br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif" id="_92_157_Open_Image" onclick="this.style.display='none'; document.getElementById('_92_157_Open_Text').style.display='none'; document.getElementById('_92_157_Closed_Image').style.display='inline'; document.getElementById('_92_157_Closed_Text').style.display='inline';" align="top" alt="" /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif" id="_92_157_Closed_Image" onclick="this.style.display='none'; document.getElementById('_92_157_Closed_Text').style.display='none'; document.getElementById('_92_157_Open_Image').style.display='inline'; document.getElementById('_92_157_Open_Text').style.display='inline';" align="top" alt="" style="DISPLAY: none" /></span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">&nbsp;Cal</span><span id="_92_157_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">...</span><span id="_92_157_Open_Text"><span style="COLOR: #000000">{<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif" id="_134_155_Open_Image" onclick="this.style.display='none'; document.getElementById('_134_155_Open_Text').style.display='none'; document.getElementById('_134_155_Closed_Image').style.display='inline'; document.getElementById('_134_155_Closed_Text').style.display='inline';" align="top" alt="" /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif" id="_134_155_Closed_Image" onclick="this.style.display='none'; document.getElementById('_134_155_Closed_Text').style.display='none'; document.getElementById('_134_155_Open_Image').style.display='inline'; document.getElementById('_134_155_Open_Text').style.display='inline';" align="top" alt="" style="DISPLAY: none" />&nbsp;&nbsp;</span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">static</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;main(String[]&nbsp;args)</span><span id="_134_155_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">...</span><span id="_134_155_Open_Text"><span style="COLOR: #000000">{<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;Simple.go();<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" alt="" />&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif" align="top" alt="" />}</span></span><span style="COLOR: #000000"><br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" alt="" /><br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" alt="" /></span></div>
</div>
调用一个静态方法就是&ldquo;类名.方法名&rdquo;,静态方法的使用很简单如上所示。一般来说，静态方法常常为应用程序中的其它类提供一些实用工具所用，在Java的类库中大量的静态方法正是出于此目的而定义的。
<p><strong>静态变量</strong></p>
<p>　　静态变量与静态方法类似。所有此类实例共享此静态变量，也就是说在类装载时，只分配一块存储空间，所有此类的对象都可以操控此块存储空间，当然对于final则另当别论了。看下面这段代码：</p>
<div style="BORDER-RIGHT: windowtext 0.5pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 0.5pt solid; PADDING-LEFT: 5.4pt; BACKGROUND: #e6e6e6; PADDING-BOTTOM: 4px; BORDER-LEFT: windowtext 0.5pt solid; WIDTH: 95%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: windowtext 0.5pt solid">
<div><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif" id="_11_65_Open_Image" onclick="this.style.display='none'; document.getElementById('_11_65_Open_Text').style.display='none'; document.getElementById('_11_65_Closed_Image').style.display='inline'; document.getElementById('_11_65_Closed_Text').style.display='inline';" align="top" alt="" /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif" id="_11_65_Closed_Image" onclick="this.style.display='none'; document.getElementById('_11_65_Closed_Text').style.display='none'; document.getElementById('_11_65_Open_Image').style.display='inline'; document.getElementById('_11_65_Open_Text').style.display='inline';" align="top" alt="" style="DISPLAY: none" /><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">&nbsp;Value</span><span id="_11_65_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">...</span><span id="_11_65_Open_Text"><span style="COLOR: #000000">{<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;</span><span style="COLOR: #0000ff">static</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;c</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif" id="_50_63_Open_Image" onclick="this.style.display='none'; document.getElementById('_50_63_Open_Text').style.display='none'; document.getElementById('_50_63_Closed_Image').style.display='inline'; document.getElementById('_50_63_Closed_Text').style.display='inline';" align="top" alt="" /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif" id="_50_63_Closed_Image" onclick="this.style.display='none'; document.getElementById('_50_63_Closed_Text').style.display='none'; document.getElementById('_50_63_Open_Image').style.display='inline'; document.getElementById('_50_63_Open_Text').style.display='inline';" align="top" alt="" style="DISPLAY: none" />&nbsp;&nbsp;</span><span style="COLOR: #0000ff">static</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;inc()</span><span id="_50_63_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">...</span><span id="_50_63_Open_Text"><span style="COLOR: #000000">{<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;c</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">;<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" alt="" />&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif" align="top" alt="" />}</span></span><span style="COLOR: #000000"><br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif" id="_78_342_Open_Image" onclick="this.style.display='none'; document.getElementById('_78_342_Open_Text').style.display='none'; document.getElementById('_78_342_Closed_Image').style.display='inline'; document.getElementById('_78_342_Closed_Text').style.display='inline';" align="top" alt="" /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif" id="_78_342_Closed_Image" onclick="this.style.display='none'; document.getElementById('_78_342_Closed_Text').style.display='none'; document.getElementById('_78_342_Open_Image').style.display='inline'; document.getElementById('_78_342_Open_Text').style.display='inline';" align="top" alt="" style="DISPLAY: none" /></span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">&nbsp;Count</span><span id="_78_342_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">...</span><span id="_78_342_Open_Text"><span style="COLOR: #000000">{<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif" id="_114_145_Open_Image" onclick="this.style.display='none'; document.getElementById('_114_145_Open_Text').style.display='none'; document.getElementById('_114_145_Closed_Image').style.display='inline'; document.getElementById('_114_145_Closed_Text').style.display='inline';" align="top" alt="" /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif" id="_114_145_Closed_Image" onclick="this.style.display='none'; document.getElementById('_114_145_Closed_Text').style.display='none'; document.getElementById('_114_145_Open_Image').style.display='inline'; document.getElementById('_114_145_Open_Text').style.display='inline';" align="top" alt="" style="DISPLAY: none" />&nbsp;&nbsp;</span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">static</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;prt(String&nbsp;s)</span><span id="_114_145_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">...</span><span id="_114_145_Open_Text"><span style="COLOR: #000000">{<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(s);<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" alt="" />&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif" id="_187_340_Open_Image" onclick="this.style.display='none'; document.getElementById('_187_340_Open_Text').style.display='none'; document.getElementById('_187_340_Closed_Image').style.display='inline'; document.getElementById('_187_340_Closed_Text').style.display='inline';" align="top" alt="" /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif" id="_187_340_Closed_Image" onclick="this.style.display='none'; document.getElementById('_187_340_Closed_Text').style.display='none'; document.getElementById('_187_340_Open_Image').style.display='inline'; document.getElementById('_187_340_Open_Text').style.display='inline';" align="top" alt="" style="DISPLAY: none" />&nbsp;&nbsp;</span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">static</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;main(String[]&nbsp;args)</span><span id="_187_340_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">...</span><span id="_187_340_Open_Text"><span style="COLOR: #000000">{<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;Value&nbsp;v1,v2;<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;v1</span><span style="COLOR: #000000">=</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000">&nbsp;Value();<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;v2</span><span style="COLOR: #000000">=</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000">&nbsp;Value();<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;prt(</span><span style="COLOR: #000000">&quot;</span><span style="COLOR: #000000">v1.c=</span><span style="COLOR: #000000">&quot;</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">v1.c</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">&quot;</span><span style="COLOR: #000000">&nbsp;&nbsp;v2.c=</span><span style="COLOR: #000000">&quot;</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">v2.c);<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;v1.inc();<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;prt(</span><span style="COLOR: #000000">&quot;</span><span style="COLOR: #000000">v1.c=</span><span style="COLOR: #000000">&quot;</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">v1.c</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">&quot;</span><span style="COLOR: #000000">&nbsp;&nbsp;v2.c=</span><span style="COLOR: #000000">&quot;</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">v2.c);&nbsp;&nbsp;<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" alt="" />&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif" align="top" alt="" />}</span></span><span style="COLOR: #000000"><br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" alt="" /></span></div>
</div>
结果如下：
<p>v1.c=0&nbsp; v2.c=0<br />v1.c=1&nbsp; v2.c=1</p>
<p>　由此可以证明它们共享一块存储区。static变量有点类似于C中的全局变量的概念。值得探讨的是静态变量的初始化问题。我们修改上面的程序：</p>
<div style="BORDER-RIGHT: windowtext 0.5pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 0.5pt solid; PADDING-LEFT: 5.4pt; BACKGROUND: #e6e6e6; PADDING-BOTTOM: 4px; BORDER-LEFT: windowtext 0.5pt solid; WIDTH: 95%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: windowtext 0.5pt solid">
<div><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif" id="_11_119_Open_Image" onclick="this.style.display='none'; document.getElementById('_11_119_Open_Text').style.display='none'; document.getElementById('_11_119_Closed_Image').style.display='inline'; document.getElementById('_11_119_Closed_Text').style.display='inline';" align="top" alt="" /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif" id="_11_119_Closed_Image" onclick="this.style.display='none'; document.getElementById('_11_119_Closed_Text').style.display='none'; document.getElementById('_11_119_Open_Image').style.display='inline'; document.getElementById('_11_119_Open_Text').style.display='inline';" align="top" alt="" style="DISPLAY: none" /><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">&nbsp;Value</span><span id="_11_119_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">...</span><span id="_11_119_Open_Text"><span style="COLOR: #000000">{<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;</span><span style="COLOR: #0000ff">static</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;c</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif" id="_40_54_Open_Image" onclick="this.style.display='none'; document.getElementById('_40_54_Open_Text').style.display='none'; document.getElementById('_40_54_Closed_Image').style.display='inline'; document.getElementById('_40_54_Closed_Text').style.display='inline';" align="top" alt="" /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif" id="_40_54_Closed_Image" onclick="this.style.display='none'; document.getElementById('_40_54_Closed_Text').style.display='none'; document.getElementById('_40_54_Open_Image').style.display='inline'; document.getElementById('_40_54_Open_Text').style.display='inline';" align="top" alt="" style="DISPLAY: none" />&nbsp;&nbsp;Value()</span><span id="_40_54_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">...</span><span id="_40_54_Open_Text"><span style="COLOR: #000000">{<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;c</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">15</span><span style="COLOR: #000000">;<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" alt="" />&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif" id="_70_83_Open_Image" onclick="this.style.display='none'; document.getElementById('_70_83_Open_Text').style.display='none'; document.getElementById('_70_83_Closed_Image').style.display='inline'; document.getElementById('_70_83_Closed_Text').style.display='inline';" align="top" alt="" /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif" id="_70_83_Closed_Image" onclick="this.style.display='none'; document.getElementById('_70_83_Closed_Text').style.display='none'; document.getElementById('_70_83_Open_Image').style.display='inline'; document.getElementById('_70_83_Open_Text').style.display='inline';" align="top" alt="" style="DISPLAY: none" />&nbsp;&nbsp;Value(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;i)</span><span id="_70_83_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">...</span><span id="_70_83_Open_Text"><span style="COLOR: #000000">{<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;c</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">i;<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" alt="" />&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif" id="_104_117_Open_Image" onclick="this.style.display='none'; document.getElementById('_104_117_Open_Text').style.display='none'; document.getElementById('_104_117_Closed_Image').style.display='inline'; document.getElementById('_104_117_Closed_Text').style.display='inline';" align="top" alt="" /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif" id="_104_117_Closed_Image" onclick="this.style.display='none'; document.getElementById('_104_117_Closed_Text').style.display='none'; document.getElementById('_104_117_Open_Image').style.display='inline'; document.getElementById('_104_117_Open_Text').style.display='inline';" align="top" alt="" style="DISPLAY: none" />&nbsp;&nbsp;</span><span style="COLOR: #0000ff">static</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;inc()</span><span id="_104_117_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">...</span><span id="_104_117_Open_Text"><span style="COLOR: #000000">{<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;c</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">;<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" alt="" />&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif" align="top" alt="" />}</span></span><span style="COLOR: #000000"><br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif" id="_132_651_Open_Image" onclick="this.style.display='none'; document.getElementById('_132_651_Open_Text').style.display='none'; document.getElementById('_132_651_Closed_Image').style.display='inline'; document.getElementById('_132_651_Closed_Text').style.display='inline';" align="top" alt="" /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif" id="_132_651_Closed_Image" onclick="this.style.display='none'; document.getElementById('_132_651_Closed_Text').style.display='none'; document.getElementById('_132_651_Open_Image').style.display='inline'; document.getElementById('_132_651_Open_Text').style.display='inline';" align="top" alt="" style="DISPLAY: none" /></span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">&nbsp;Count</span><span id="_132_651_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">...</span><span id="_132_651_Open_Text"><span style="COLOR: #000000">{<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif" id="_168_199_Open_Image" onclick="this.style.display='none'; document.getElementById('_168_199_Open_Text').style.display='none'; document.getElementById('_168_199_Closed_Image').style.display='inline'; document.getElementById('_168_199_Closed_Text').style.display='inline';" align="top" alt="" /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif" id="_168_199_Closed_Image" onclick="this.style.display='none'; document.getElementById('_168_199_Closed_Text').style.display='none'; document.getElementById('_168_199_Open_Image').style.display='inline'; document.getElementById('_168_199_Open_Text').style.display='inline';" align="top" alt="" style="DISPLAY: none" />&nbsp;&nbsp;</span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">static</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;prt(String&nbsp;s)</span><span id="_168_199_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">...</span><span id="_168_199_Open_Text"><span style="COLOR: #000000">{<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(s);<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" alt="" />&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;Value&nbsp;v</span><span style="COLOR: #000000">=</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000">&nbsp;Value(</span><span style="COLOR: #000000">10</span><span style="COLOR: #000000">);<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">static</span><span style="COLOR: #000000">&nbsp;Value&nbsp;v1,v2;<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif" id="_262_436_Open_Image" onclick="this.style.display='none'; document.getElementById('_262_436_Open_Text').style.display='none'; document.getElementById('_262_436_Closed_Image').style.display='inline'; document.getElementById('_262_436_Closed_Text').style.display='inline';" align="top" alt="" /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif" id="_262_436_Closed_Image" onclick="this.style.display='none'; document.getElementById('_262_436_Closed_Text').style.display='none'; document.getElementById('_262_436_Open_Image').style.display='inline'; document.getElementById('_262_436_Open_Text').style.display='inline';" align="top" alt="" style="DISPLAY: none" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">static</span><span id="_262_436_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">...</span><span id="_262_436_Open_Text"><span style="COLOR: #000000">{<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;prt(</span><span style="COLOR: #000000">&quot;</span><span style="COLOR: #000000">v1.c=</span><span style="COLOR: #000000">&quot;</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">v1.c</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">&quot;</span><span style="COLOR: #000000">&nbsp;&nbsp;v2.c=</span><span style="COLOR: #000000">&quot;</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">v2.c);<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;v1</span><span style="COLOR: #000000">=</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000">&nbsp;Value(</span><span style="COLOR: #000000">27</span><span style="COLOR: #000000">);<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;prt(</span><span style="COLOR: #000000">&quot;</span><span style="COLOR: #000000">v1.c=</span><span style="COLOR: #000000">&quot;</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">v1.c</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">&quot;</span><span style="COLOR: #000000">&nbsp;&nbsp;v2.c=</span><span style="COLOR: #000000">&quot;</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">v2.c);<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;v2</span><span style="COLOR: #000000">=</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000">&nbsp;Value(</span><span style="COLOR: #000000">15</span><span style="COLOR: #000000">);<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;prt(</span><span style="COLOR: #000000">&quot;</span><span style="COLOR: #000000">v1.c=</span><span style="COLOR: #000000">&quot;</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">v1.c</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">&quot;</span><span style="COLOR: #000000">&nbsp;&nbsp;v2.c=</span><span style="COLOR: #000000">&quot;</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">v2.c);<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" /><br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif" id="_479_649_Open_Image" onclick="this.style.display='none'; document.getElementById('_479_649_Open_Text').style.display='none'; document.getElementById('_479_649_Closed_Image').style.display='inline'; document.getElementById('_479_649_Closed_Text').style.display='inline';" align="top" alt="" /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif" id="_479_649_Closed_Image" onclick="this.style.display='none'; document.getElementById('_479_649_Closed_Text').style.display='none'; document.getElementById('_479_649_Open_Image').style.display='inline'; document.getElementById('_479_649_Open_Text').style.display='inline';" align="top" alt="" style="DISPLAY: none" />&nbsp;&nbsp;</span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">static</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;main(String[]&nbsp;args)</span><span id="_479_649_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">...</span><span id="_479_649_Open_Text"><span style="COLOR: #000000">{<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;Count&nbsp;ct</span><span style="COLOR: #000000">=</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000">&nbsp;Count();<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;prt(</span><span style="COLOR: #000000">&quot;</span><span style="COLOR: #000000">ct.c=</span><span style="COLOR: #000000">&quot;</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">ct.v.c);<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;prt(</span><span style="COLOR: #000000">&quot;</span><span style="COLOR: #000000">v1.c=</span><span style="COLOR: #000000">&quot;</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">v1.c</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">&quot;</span><span style="COLOR: #000000">&nbsp;&nbsp;v2.c=</span><span style="COLOR: #000000">&quot;</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">v2.c);<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;v1.inc();<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;prt(</span><span style="COLOR: #000000">&quot;</span><span style="COLOR: #000000">v1.c=</span><span style="COLOR: #000000">&quot;</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">v1.c</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">&quot;</span><span style="COLOR: #000000">&nbsp;&nbsp;v2.c=</span><span style="COLOR: #000000">&quot;</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">v2.c);<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;prt(</span><span style="COLOR: #000000">&quot;</span><span style="COLOR: #000000">ct.c=</span><span style="COLOR: #000000">&quot;</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">ct.v.c);<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" alt="" />&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif" align="top" alt="" />}</span></span><span style="COLOR: #000000"><br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" alt="" /><br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" align="top" alt="" /></span></div>
</div>
<p>运行结果如下：</p>
<p>v1.c=0&nbsp; v2.c=0<br />v1.c=27&nbsp; v2.c=27<br />v1.c=15&nbsp; v2.c=15<br />ct.c=10<br />v1.c=10&nbsp; v2.c=10<br />v1.c=11&nbsp; v2.c=11<br />ct.c=11</p>
<p>　　这个程序展示了静态初始化的各种特性。如果你初次接触Java，结果可能令你吃惊。可能会对static后加大括号感到困惑。首先要告诉你的是，static定义的变量会优先于任何其它非static变量，不论其出现的顺序如何。正如在程序中所表现的，虽然v出现在v1和v2的前面，但是结果却是v1和v2的初始化在v的前面。在static{后面跟着一段代码，这是用来进行显式的静态变量初始化，这段代码只会初始化一次，且在类被第一次装载时。如果你能读懂并理解这段代码，会帮助你对static关键字的认识。在涉及到继承的时候，会先初始化父类的static变量，然后是子类的，依次类推。非静态变量不是本文的主题，在此不做详细讨论，请参考Think in Java中的讲解。</p>
<p><strong>静态类</strong></p>
<p>　　通常一个普通类不允许声明为静态的，只有一个内部类才可以。这时这个声明为静态的内部类可以直接作为一个普通类来使用，而不需实例一个外部类。如下代码所示：</p>
<div style="BORDER-RIGHT: windowtext 0.5pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 0.5pt solid; PADDING-LEFT: 5.4pt; BACKGROUND: #e6e6e6; PADDING-BOTTOM: 4px; BORDER-LEFT: windowtext 0.5pt solid; WIDTH: 95%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: windowtext 0.5pt solid">
<div><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif" id="_22_120_Open_Image" onclick="this.style.display='none'; document.getElementById('_22_120_Open_Text').style.display='none'; document.getElementById('_22_120_Closed_Image').style.display='inline'; document.getElementById('_22_120_Closed_Text').style.display='inline';" align="top" alt="" /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif" id="_22_120_Closed_Image" onclick="this.style.display='none'; document.getElementById('_22_120_Closed_Text').style.display='none'; document.getElementById('_22_120_Open_Image').style.display='inline'; document.getElementById('_22_120_Open_Text').style.display='inline';" align="top" alt="" style="DISPLAY: none" /><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">&nbsp;StaticCls</span><span id="_22_120_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">...</span><span id="_22_120_Open_Text"><span style="COLOR: #000000">{<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif" id="_64_118_Open_Image" onclick="this.style.display='none'; document.getElementById('_64_118_Open_Text').style.display='none'; document.getElementById('_64_118_Closed_Image').style.display='inline'; document.getElementById('_64_118_Closed_Text').style.display='inline';" align="top" alt="" /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif" id="_64_118_Closed_Image" onclick="this.style.display='none'; document.getElementById('_64_118_Closed_Text').style.display='none'; document.getElementById('_64_118_Open_Image').style.display='inline'; document.getElementById('_64_118_Open_Text').style.display='inline';" align="top" alt="" style="DISPLAY: none" />&nbsp;&nbsp;</span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">static</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;main(String[]&nbsp;args)</span><span id="_64_118_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">...</span><span id="_64_118_Open_Text"><span style="COLOR: #000000">{<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;OuterCls.InnerCls&nbsp;oi</span><span style="COLOR: #000000">=</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000">&nbsp;OuterCls.InnerCls();<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" alt="" />&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif" align="top" alt="" />}</span></span><span style="COLOR: #000000"><br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif" id="_136_235_Open_Image" onclick="this.style.display='none'; document.getElementById('_136_235_Open_Text').style.display='none'; document.getElementById('_136_235_Closed_Image').style.display='inline'; document.getElementById('_136_235_Closed_Text').style.display='inline';" align="top" alt="" /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif" id="_136_235_Closed_Image" onclick="this.style.display='none'; document.getElementById('_136_235_Closed_Text').style.display='none'; document.getElementById('_136_235_Open_Image').style.display='inline'; document.getElementById('_136_235_Open_Text').style.display='inline';" align="top" alt="" style="DISPLAY: none" /></span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">&nbsp;OuterCls</span><span id="_136_235_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">...</span><span id="_136_235_Open_Text"><span style="COLOR: #000000">{<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif" id="_168_233_Open_Image" onclick="this.style.display='none'; document.getElementById('_168_233_Open_Text').style.display='none'; document.getElementById('_168_233_Closed_Image').style.display='inline'; document.getElementById('_168_233_Closed_Text').style.display='inline';" align="top" alt="" /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif" id="_168_233_Closed_Image" onclick="this.style.display='none'; document.getElementById('_168_233_Closed_Text').style.display='none'; document.getElementById('_168_233_Open_Image').style.display='inline'; document.getElementById('_168_233_Open_Text').style.display='inline';" align="top" alt="" style="DISPLAY: none" />&nbsp;&nbsp;</span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">static</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">&nbsp;InnerCls</span><span id="_168_233_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">...</span><span id="_168_233_Open_Text"><span style="COLOR: #000000">{<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif" id="_184_228_Open_Image" onclick="this.style.display='none'; document.getElementById('_184_228_Open_Text').style.display='none'; document.getElementById('_184_228_Closed_Image').style.display='inline'; document.getElementById('_184_228_Closed_Text').style.display='inline';" align="top" alt="" /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif" id="_184_228_Closed_Image" onclick="this.style.display='none'; document.getElementById('_184_228_Closed_Text').style.display='none'; document.getElementById('_184_228_Open_Image').style.display='inline'; document.getElementById('_184_228_Open_Text').style.display='inline';" align="top" alt="" style="DISPLAY: none" />&nbsp;&nbsp;&nbsp;&nbsp;InnerCls()</span><span id="_184_228_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">...</span><span id="_184_228_Open_Text"><span style="COLOR: #000000">{<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(</span><span style="COLOR: #000000">&quot;</span><span style="COLOR: #000000">InnerCls</span><span style="COLOR: #000000">&quot;</span><span style="COLOR: #000000">);<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif" align="top" alt="" />}</span></span></div>
</div>
输出结果会如你所料：
<p>InnerCls</p>
<p>　　和普通类一样。内部类的其它用法请参阅Think in Java中的相关章节，此处不作详解。</p>
<p>二、this &amp; super</p>
<p>　　在上一篇拙作中，我们讨论了static的种种用法，通过用static来定义方法或成员，为我们编程提供了某种便利，从某种程度上可以说它类似于C语言中的全局函数和全局变量。但是，并不是说有了这种便利，你便可以随处使用，如果那样的话，你便需要认真考虑一下自己是否在用面向对象的思想编程，自己的程序是否是面向对象的。好了，现在开始讨论this&amp;super这两个关键字的意义和用法。</p>
<p>　　在Java中，this通常指当前对象，super则指父类的。当你想要引用当前对象的某种东西，比如当前对象的某个方法，或当前对象的某个成员，你便可以利用this来实现这个目的，当然，this的另一个用途是调用当前对象的另一个构造函数，这些马上就要讨论。如果你想引用父类的某种东西，则非super莫属。由于this与super有如此相似的一些特性和与生俱来的某种关系，所以我们在这一块儿来讨论，希望能帮助你区分和掌握它们两个。</p>
<p><strong>在一般方法中</strong></p>
<p>　　最普遍的情况就是，在你的方法中的某个形参名与当前对象的某个成员有相同的名字，这时为了不至于混淆，你便需要明确使用this关键字来指明你要使用某个成员，使用方法是&ldquo;this.成员名&rdquo;，而不带this的那个便是形参。另外，还可以用&ldquo;this.方法名&rdquo;来引用当前对象的某个方法，但这时this就不是必须的了，你可以直接用方法名来访问那个方法，编译器会知道你要调用的是那一个。下面的代码演示了上面的用法：</p>
<div style="BORDER-RIGHT: windowtext 0.5pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 0.5pt solid; PADDING-LEFT: 5.4pt; BACKGROUND: #e6e6e6; PADDING-BOTTOM: 4px; BORDER-LEFT: windowtext 0.5pt solid; WIDTH: 95%; WORD-BREAK: break-all; PADDING-TOP