虾's profileSavor the blue ...PhotosBlogLists Tools Help

Blog


    March 23

    Java中的静态概念

    静态方法

      通常,在一个类中定义一个方法为static,那就是说,无需本类的对象即可调用此方法。如下所示:

    class Simple{
       static void go(){
         System.out.println("Go...");
       }
    }
    public class Cal{
      public static void main(String[] args){
        Simple.go();
      }
    }

      调用一个静态方法就是“类名.方法名”,静态方法的使用很简单如上所示。一般来说,静态方法常常为应用程序中的其它类提供一些实用工具所用,在Java的类库中大量的静态方法正是出于此目的而定义的。

     

    静态变量

      静态变量与静态方法类似。所有此类实例共享此静态变量,也就是说在类装载时,只分配一块存储空间,所有此类的对象都可以操控此块存储空间,当然对于final则另当别论了。看下面这段代码:

    class Value{
      static int c=0;
      static void inc(){
        c++;
      }
    }
    class Count{
      public static void prt(String s){
        System.out.println(s);
      }
      public static void main(String[] args){
        Value v1,v2;
        v1=new Value();
        v2=new Value();
        prt("v1.c="+v1.c+"  v2.c="+v2.c);
        v1.inc();
        prt("v1.c="+v1.c+"  v2.c="+v2.c); 
      }
    }

      结果如下:

    v1.c=0  v2.c=0
    v1.c=1  v2.c=1

      由此可以证明它们共享一块存储区。static变量有点类似于C中的全局变量的概念。值得探讨的是静态变量的初始化问题。我们修改上面的程序:

    class Value{
      static int c=0;
      Value(){
        c=15;
      }
      Value(int i){
        c=i;
      }
      static void inc(){
        c++;
      }
    }
    class Count{
      public static void prt(String s){
        System.out.println(s);
      }
        Value v=new Value(10);
        static Value v1,v2;
        static{
          prt("v1.c="+v1.c+"  v2.c="+v2.c);
          v1=new Value(27);
          prt("v1.c="+v1.c+"  v2.c="+v2.c);
          v2=new Value(15);
          prt("v1.c="+v1.c+"  v2.c="+v2.c);
        }

      public static void main(String[] args){
        Count ct=new Count();
        prt("ct.c="+ct.v.c);
        prt("v1.c="+v1.c+"  v2.c="+v2.c);
        v1.inc();
        prt("v1.c="+v1.c+"  v2.c="+v2.c);
        prt("ct.c="+ct.v.c);
      }
    }

    运行结果如下:

    v1.c=0  v2.c=0
    v1.c=27  v2.c=27
    v1.c=15  v2.c=15
    ct.c=10
    v1.c=10  v2.c=10
    v1.c=11  v2.c=11
    ct.c=11

      这个程序展示了静态初始化的各种特性。如果你初次接触Java,结果可能令你吃惊。可能会对static后加大括号感到困惑。首先要告诉你的是,static定义的变量会优先于任何其它非static变量,不论其出现的顺序如何。正如在程序中所表现的,虽然v出现在v1和v2的前面,但是结果却是v1和v2的初始化在v的前面。在static{后面跟着一段代码,这是用来进行显式的静态变量初始化,这段代码只会初始化一次,且在类被第一次装载时。如果你能读懂并理解这段代码,会帮助你对static关键字的认识。在涉及到继承的时候,会先初始化父类的static变量,然后是子类的,依次类推。非静态变量不是本文的主题,在此不做详细讨论,请参考Think in Java中的讲解。

    静态类

      通常一个普通类不允许声明为静态的,只有一个内部类才可以。这时这个声明为静态的内部类可以直接作为一个普通类来使用,而不需实例一个外部类。如下代码所示:

    public class StaticCls{
      public static void main(String[] args){
        OuterCls.InnerCls oi=new OuterCls.InnerCls();
      }
    }
    class OuterCls{
      public static class InnerCls{
        InnerCls(){
          System.out.println("InnerCls");
        }
       }
    }

      输出结果会如你所料:

    InnerCls

      和普通类一样。内部类的其它用法请参阅Think in Java中的相关章节,此处不作详解。

    Comments

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.

    To add a comment, sign in with your Windows Live ID (if you use Hotmail, Messenger, or Xbox LIVE, you have a Windows Live ID). Sign in


    Don't have a Windows Live ID? Sign up

    Trackbacks

    The trackback URL for this entry is:
    http://javainthinking.spaces.live.com/blog/cns!B2988DE9678DF674!112.trak
    Weblogs that reference this entry
    • None