zoukankan      html  css  js  c++  java
  • static 方法

    static 方法就是没有this 的方法,在static 内部不能调用非静态方法,反过来倒是可以,而且可以在没有创建任何其他对象的前提下,仅仅通过类本身调用static 方法,

    static 成员初始化顺序

    package object;
    
    class Bowl {
        Bowl(int marker)
        {
            System.out.printf("Bowl("+marker+")
    ");
        }
        void f1(int marker)
        {
            System.out.printf("f1("+marker+")
    ");
        }
    }
    class Table{
        static Bowl bowl1 =new Bowl(1);
        Table()
        {
            System.out.println("table()");
        }
        void f2(int marker)
        {
            System.out.println("f2("+marker+")");
        }
        static Bowl bowl2 = new Bowl(2);
    }
    class Cupboard
    {
        Bowl  bowl3 = new Bowl(3);
        static Bowl bowl4 = new Bowl(4);
        Cupboard()
        {
            System.out.print("Cupboard
    ");
            bowl4.f1(2);
        }
        void f3(int marker)
        {
            System.out.println("f3("+marker+")");
        }
        static Bowl bowl5 = new Bowl(5);
    }
    public class StaticInitialization{
        public static void main(String args[])
        {
            System.out.println("Creating new Cupboard() in main");
            new Cupboard();  //静态成员只有在第一个Cupboard在创建时才会初始化,此后,静态成员不会再次初始化
            System.out.println("Creating new Cupboard() in main");
            Cupboard cupboard = new Cupboard();
            new Table().f2(1);
            new Cupboard().f3(1);
        }
    }/* output:
    Creating new Cupboard() in main
    Bowl(4)
    Bowl(5)
    Bowl(3)
    Cupboard
    f1(2)
    Creating new Cupboard() in main
    Bowl(3)
    Cupboard
    f1(2)
    Bowl(1)
    Bowl(2)
    table()
    f2(1)
    Bowl(3)
    Cupboard
    f1(2)
    f3(1)
    *///~

    显示的初始化静态方法

    package object;
    //: initialization/ExplicitStatic.java
    
    import static net.mindview.util.Print.*;
    
    class Cup
    {
        Cup(int marker)
        {    
            print("Cup("+ marker +")");
        }
        void f(int marker)
        {
            print("f("+marker+")");
        }
    }
    class Cups
    {
        static Cup cup1;
        static Cup cup2; //静态域
        static{
            cup1 = new Cup(1);
            cup2 = new Cup(2);
        }    //静态块
        Cups()
        {
            print("Cups()");
        }
    }
    
    public class ExplicitStatic{
        public static void main(String args[])
        {
            print("Inside main()");
            Cups.cup1.f(99); // (1)     无论时通过(1)还是注释掉(1)运行(2),Cups的静态初始化都会执行
    // 静态初始化只会执行一次,如果(1)(2)全部注释掉则不会执行初始化
    } //static Cups cups1 = new Cups(); //(2) //static Cups cups2 = new Cups(); //(2) }/* output: Inside main() Cup(1) Cup(2) f(99) *///~
  • 相关阅读:
    OPC客户端的进程安全初始化
    [精华] Oracle安装(linux)总结一下[转]
    Linux防火墙iptables的设置与启动[转]
    Linux Server 5.5安装SVN+Apache服务[转]
    Red hat Linux Enterprise 5.4 Edtion 学习笔记[二]
    RedHat Linux 5企业版开启VNCSERVER远程桌面功能[转]
    Linux服务配置:Vsftp的基本配置[转]
    Linux查看和剔除当前登录用户
    Ubuntu10.04的中文问题汇集与解决[转]
    Linux下扩展swap分区的方法
  • 原文地址:https://www.cnblogs.com/jiangfeilong/p/10060880.html
Copyright © 2011-2022 走看看