学Java的第六天(自闭一段时间开始写了)

还是关于变量的一些知识

static:
        1.修饰变量,静态变量存在静态存储区,被多个实例共享,可以用类名直接访问
            静态变量是跟随类的加载而加载。生命周期也是跟随类一起创建和消亡
        2.可以修饰方法:静态方法可以用类名直接调用,一般工具类中的方法都是静态的,方便使用。
                静态方法是跟随类的加载而加载。不能有this关键,当静态函数加载的时this对应实例还没创建。
                静态方法不能访问非静态的变量。
*/
class  JavaDemo23
{
    public static void main(String[] args) 
    {
        //System.out.println("Hello World!");
        Student stu1 = new Student();
        Student stu2 = new Student();

        System.out.println(stu1.country);
        System.out.println(stu2.country);

        System.out.println(Student.country);
        Student.country = "马来西亚";
        
        System.out.println(stu1.country);
        System.out.println(stu2.country);

        System.out.println(Student.country);

        System.out.println(Student.COUNTRY);    

        Student.test();

    }
}

class Student
{
    private String name = "香港";
    public  static String country = "中国";
    //java中的常量定义
    public final static String COUNTRY = "中国";

    public static void test(){
        //this.name = name;
        System.out.println("test"+country);
    }
}
 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值