首页 > 其他 > 详细

static

时间:2021-07-17 10:57:31      阅读:28      评论:0      收藏:0      [点我收藏+]

static int 不管在函数内还是函数外,都作为一个全局变量可以保存它被修改以后的值。

而int则没有这一功能。只有作为全局变量时能保存修改。

package com.ren;

public class Student {
    private static int age;//静态的变量  多线程
    private double score;//非静态的变量

    public void run(){

    }

    public static void go(){

    }

    public static void main(String[] args) {
        Student s1 = new Student();
        s1.score = 100;
        s1.age = 18;
        Student.age = 19;
        System.out.println(s1.age);
        Student.go();
        s1.run();
        go();
    }
}
package com.ren;

public class Person {

    {
        //匿名代码块  用于赋初始值
        System.out.println("匿名代码块");
    }

    static {
        //静态代码块  永久只执行一次
        System.out.println("静态代码块");
    }
    public Person(){
        System.out.println("构造方法");
    }

    public static void main(String[] args) {
        new Person();
        new Person();
    }
}

技术分享图片

 

package com.ren;

//静态导入包
import static java.lang.Math.random;
import static java.lang.Math.PI;

public class Test {
    public static void main(String[] args) {
        System.out.println(random());
        System.out.println(PI);
    }
}

 

static

原文:https://www.cnblogs.com/1764782550-rzs/p/15022710.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!