`
从此醉
  • 浏览: 1046504 次
  • 性别: Icon_minigender_1
  • 来自: US
社区版块
存档分类
最新评论

Java中的static

 
阅读更多
<wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)">学习本文你到底要学到什么:</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)">1、</span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)">static在java中到底代表什么,为何要用它?</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)">2、</span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)">static在java中怎么用?</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><p><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)">3、</span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)">static</span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)">有那些特点和使用的“局限”</span></wbr></wbr></p> <p><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)">1、</span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)">static在java中到底代表什么,为何要用它?</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)">static――静态――“指定位置“</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)">首先,我们来看看java的内存:java把内存分为栈内存和堆内存,栈内存用来存放一些基本类型的变量和数组及对象的引用变量,而堆内存主要是来放置对象的。</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)">用static的修饰的变量和方法,实际上是指定了这些变量和方法在内存中的“固定位置”-static</span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)">storage。既然要有“固定位置”那么他们的“大小”似乎就是固定的了,有了固定位置和固定大小的特征了,在栈中或堆中开辟空间那就是非常的方便了。如果静态的变量或方法在不出其作用域的情况下,其引用句柄是不会发生改变的。</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)">我们常看到:static变量有点类似于C中的全局变量的概念;静态表示的是内存的共享,就是它的每一个实例都指向同一个内存地址。把static拿来,就是告诉JVM它是静态的,它的引用(含间接引用)都是指向同一个位置,在那个地方,你把它改了,它就不会变成原样,你把它清理了,它就不会回来了。我们常可看到类似以下的例子来说明这个问题:</span><br></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p> <p><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"></span></span></p> <pre name="code" class="java">package com.meritit.test; class Student { static int numberOfStudents = 0; Student() { numberOfStudents++; } }</pre> <br><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)">每一次创建一个新的Student实例时,成员numberOfStudents都会不断的递增,并且所有的Student实例都访问同一个numberOfStudents变量,实际上int</span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)">numberOfStudents变量在内存中只存储在一个位置上。</span><br><p><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)">多个实例共享一个变量似乎不足以让我们对static那么的热情,实际上java引入static却有另外的含义:</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)">(1)、引用static的方法和变量,不需要和实例捆绑在一起,这可以提高代码的编写的效率,这样的例子我们随处可见;</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)">(2)、java的主类中main()方法本身就是一个static的,所以main方法的执行就是在没有产生新的实例的情况;对于静态的东西,JVM在加载类时,就在內存中开辟了这些静态的空间,所以虽没有静态的main()方法但是程序还是执行了,不过抛出来了无main()方法的异常。这也不知算不算是java的一个漏洞;</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)">(3)、如果需要创建一个脱离于实例的变量或方法(只与整个类有关),那么用static作修饰是再好不过了,如我们经常看到要统计实例实现的个数(通常的例子就是计数)。</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)">(4)、使用一种静态的方法的编程通常叫做防御(defensive)编程,它可以在API供应商突然中断支持的情况下保护代码</span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"></span><br></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></span></span></p> <p><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)">2、</span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)">static在java中怎么用?</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)">使用static时,要记着我阐述的static代表什么。</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)">static使用非常的简单,如果要修饰一个静态块只需:staic</span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)">{……..}即可(常用静态块来初始化一些变量);</span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)">静态方法就参照main()的形式:访问标识</span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)">static</span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)">returnType</span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)">method(…)</span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)">{};静态变量就是:static</span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)">type</span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)">fields;</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)">在使用静态的方法时,可以直接用类名来引用,不需要创建实例(当然创建实例也是可以的),例如,System.out,String.valueOf()等等。</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)">3、</span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)">static</span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)">有那些特点和使用的“局限”?</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)">从上面的分析可知,static的东西在类加载时,就分配了内存空间,即编译时就为这些成员变量的实例分配了空间。</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)">那么在static块内和方法体内,我们能给它放一个在内存中还没有着落的变量?显然与我们先前所说的相左。static的东西,人家是在static</span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)">storage中有“指定位置“的,如果我们茫然的在static的作用域中放置一个普通的变量,那么编译时JVM就毫不客气的给你个异常:</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)">non-static</span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)">variable</span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)">a</span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)">cannot</span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)">be</span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)">referenced</span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)">from</span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)">a</span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)">static</span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)">context或non-static</span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)">method</span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)">Test()</span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)">cannot</span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)">be</span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)">referenced</span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)">from</span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)">a</span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)">static</span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)">context(注:Test()是我试验时的一个例子),除非我在static中现场开辟空间,用new来要内存。</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)">对于static的初始化问题,我们还是值得讨论的。现看下面的例子</span><br></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></span></span></span></p> <p><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"></span></span></span></span></span></p> <pre name="code" class="java">package com.meritit.test; public class StaticInit { static int i; int a; public StaticInit() { a=6; System.out.println("a 的初始化"+a); } public static void main(String[] args) { new StaticInit(); } static { i=5; System.out.println("i 的初始化"+i); } }</pre> <br><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)">运行结果如下:</span> <p><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)">i</span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)">的初始化5</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)">a</span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)">的初始化6</span></wbr></wbr></span></span></span></span></span></p> <p><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)">静态块的初始化要早于非静态的,原因就是在于这些东西是在类装载时就开始初始化了。</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)">说起static的“局限“,总结起来就是:在static的方法中仅能够调用其他的static方法和static变量;在static方法中不能以任何方式引用this或super;static变量在定义时必须进行初始化,并且初始化的时间早于非静态。还有一个局限我需要具体的说明一下,static的变量的初始化仅能一次,如下例:</span><br></wbr></span></span></span></span></span></p> <p><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"></span></span></span></span></span></span></p> <pre name="code" class="java">package com.meritit.test; public class Static { T1 t1 = new T1(2); T1 t2 = new T1(3); T1 t3 = new T1(4); public Static() { System.out.println("t1: " + t1.t); System.out.println("t2: " + t2.t); System.out.println("t3: " + t3.t); } public static void main(String args[]) { new Static(); } } class T1 { static int t = 1; T1(int b) { t = b; } }</pre> <br><p><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)">运行结果:</span></span></span></span></span></span></p> <p><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)">t1: 4<br> t2: 4<br> t3: 4<br></span></span></span></span></span></span></p> <p><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)">该static变量只是接受了最后一次的初始化.实际这还是我们先前提到的多个实例共享一个静态的变量的问题。</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; background-color:rgb(220,220,220)">总之,static就是给我们一个这样的忠告:static的东西在编译时,就已向内存要到了存取空间了。</span><br></span></span></span></span></span></span></p> </wbr></wbr></wbr></wbr>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics