java之static用法详解(全)

 

以下的成员包括成员变量和方法

 

 

类内:

一.非静态调用非静态:

1.直接通过成员名调用;

2.通过this.成员名调用;

3.先声明本类的对象,再通过该对象名.成员名调用;

二.非静态调用静态:

1.直接通过成员名调用;

2.通过this.成员名调用;

3.先声明本类的对象,再通过该对象名.成员名调用;

4.通过本类类名.成员名调用;

三.静态调用非静态:

1.先声明本类的对象,再通过该对象名.成员名调用;

四.静态调用静态:

1.直接通过成员名调用;

2.先声明本类的对象,再通过该对象名.成员名调用;

3.通过本类类名.成员名调用;

 

 

类外:

一.调用非静态:

1.先声明该类的对象,再通过该对象名.成员名调用;

二.调用静态

1.先声明该类的对象,再通过该对象名.成员名调用;

2.通过该类类名.成员名调用;

 

 

例:

package statictest;

 

public class classtest {

    int n;//非静态成员变量n

    static int m;//静态成员变量m

    public void f(){}//非静态方法f()

    public static void g(){}//静态方法g()

    public void test1(){

        System.out.println(n);//类内之非静态调用非静态之1

        System.out.println(this.n);//类内之非静态调用非静态之2

        classtest ct1=new classtest();

        System.out.println(ct1.n);//类内之非静态调用非静态之3

//        System.out.println(classtest.n);//错误

 

        System.out.println(m);//类内之非静态调用静态之1

        System.out.println(this.m);//类内之非静态调用静态之2

        classtest ct2=new classtest();

        System.out.println(ct2.m);//类内之非静态调用静态之3

        System.out.println(classtest.m);//类内之非静态调用静态之4

 

        f();//类内之非静态调用非静态之1

        this.f();//类内之非静态调用非静态之2

        classtest ct3=new classtest();

        ct3.f();//类内之非静态调用非静态之3

//        classtest.f();//错误

 

 

        g();//类内之非静态调用静态之1

        this.g();//类内之非静态调用静态之2

        classtest ct4=new classtest();

        ct4.g();//类内之非静态调用静态之3

        classtest.g();//类内之非静态调用静态之4

 

    }

 

    public  static void test2(){

//        System.out.println(n);//错误

//       System.out.println(this.n);//错误

        classtest ct1=new classtest();

        System.out.println(ct1.n);//类内之静态调用非静态之1

//        System.out.println(classtest.n);//错误

 

        System.out.println(m);//类内之静态调用静态之1

//        System.out.println(this.m);//错误

        classtest ct2=new classtest();

        System.out.println(ct2.m);//类内之静态调用静态之2

        System.out.println(classtest.m);//类内之静态调用静态之3

 

//        f();//错误

//        this.f();//错误

        classtest ct3=new classtest();

        ct3.f();//类内之静态调用非静态之1

//        classtest.f();//错误

 

 

        g();//类内之静态调用静态之1

//        this.g();//错误

        classtest ct4=new classtest();

        ct4.g();//类内之静态调用静态之2

        classtest.g();//类内之静态调用静态之3

 

    }

}

 

}

 

package statictest;

 

public class maintest {

    public static void main(String[] args) {

        classtest ct1=new classtest();

 

        System.out.println(ct1.n);//类外之调用非静态之1

//        System.out.println(classtest.n);//错误

        System.out.println(ct1.m);//类外之调用静态之1

        System.out.println(classtest.m);//类外之调用静态之2

 

 

 

 

 

         classtest ct2=new classtest();

         ct2.test1();//类外之调用非静态之1

        // classtest.test1();//错误

        ct2.test2();//类外之调用静态之1

        classtest.test2();//类外之调用静态之2

    }

 

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值