Java中static关键字特点以及注意

static基本概念
static 意思是静态的,可以用来修饰成员变量和方法。

static关键字的特点

1.一个类中被static所修饰的成员被类的所有对象共享

package org.wanyong.test;


/*
*
* 先创建一个学生类
* 定义两个成员变量
* 其中country被static修饰
*
* */
public class Student {
    public String name;
    public static String country;
}

package org.wanyong.test;

/*
* 创建一个测试类
* */

public class StudentTest {
    public static void main(String[] args) {
        //创建三个学生对象
        Student st1 = new Student();
        Student st2 = new Student();
        Student st3 = new Student();
        //st1 给name和country赋值
        st1.name="王小明";
        st1.country="中国";
        //打印
        System.out.println(st1.name+"---"+st1.country);
        System.out.println(st2.name+"---"+st2.country);
        System.out.println(st3.name+"---"+st3.country);
    }
}

结果为:
王小明—中国
null—中国
null—中国

我们可以发现,测是类中只对st1中的name和country,但是st2.country和st3.country也打印了"中国",而且只有st1.name打印了"王小明"其他两个对象的name均没有被赋值,然而学生类的成员变量name和country的区别是前者没有被static修饰后者被static修饰了。

我们将测测试类的代码稍作修改

package org.wanyong.test;

/*
* 创建一个测试类
* */

public class StudentTest {
    public static void main(String[] args) {
        //创建三个学生对象
        Student st1 = new Student();
        Student st2 = new Student();
        Student st3 = new Student();
        //st1 给name和country赋值
        st1.name="王小明";
        st1.country="中国";
        //st2 给name和country赋值
        st2.name="张大花";
        st2.country="美国";
        //st3 给name和country赋值
        st3.name="王二小";
        st3.country="法国";
        //打印
        System.out.println(st1.name+"---"+st1.country);
        System.out.println(st2.name+"---"+st2.country);
        System.out.println(st3.name+"---"+st3.country);
    }
}

结果为:
王小明—法国
张大花—法国
王二小—法国

再稍作修改

package org.wanyong.test;

/*
* 创建一个测试类
* */

public class StudentTest {
    public static void main(String[] args) {
        //创建三个学生对象
        Student st1 = new Student();
        Student st2 = new Student();
        Student st3 = new Student();
        //st1 给name和country赋值
        st1.name="王小明";
        st1.country="中国";
        //st2 给name和country赋值
        st2.name="张大花";
        st2.country="美国";
        //打印
        System.out.println(st1.name+"---"+st1.Country);
        System.out.println(st2.name+"---"+st2.Country);
        //st3 给name和country赋值
        st3.name="王二小";
        st3.country="法国";
        //打印
        System.out.println(st3.name+"---"+st3.Country);
    }
}

结果为:
王小明—美国
张大花—美国
王二小—法国

从上述例子中我们不难看出,被static修饰的成员变量country是三个对象共用的。

所以我们可以得出static关键字的特点:一个类中被static所修饰的成员被类的所有对象共享。

下面我们用内存图来帮助理解(途中的测试类是上述测试类的第二个)

图中的共享区又称静态区

在这里插入图片描述

2. 随着类的加载而加载,所有优先于对象的存在
举例:

package org.wanyong.test;


/*
*
* 先创建一个学生类
* 定义两个成员变量
*
* */
public class Student {
    public String name="王小明";
    public String country="中国";
    //打印name
    public void showName(){
        System.out.println(name);
    }
    //打印country
    public static void  showCountry(){
        System.out.println(country);
    }
}

该段代码第19行country报错,因为showCountry方法被static修饰,所以他随类的加载而加载,所以无发找到country。

3.可以通过类名调用
其实它本身也可以通过对象名调用。
我们也推荐使用类名调用。
静态修饰的内容一般我们称其为:与类相关的,类成员。

static注意事项
1.在静态方法中是没有this关键字的
静态是随着类的加载而加载,this是随着对象的创建而存在。
静态比对象先存在。

2.静态方法只能访问静态的成员变量和静态的成员方法
静态只能访问静态,非静态可以访问静态的也可以访问非静态的。

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值