首页 > 其他 > 详细

static关键字

时间:2021-06-21 15:28:10      阅读:19      评论:0      收藏:0      [点我收藏+]
package com.oop.demo05;

/**
 * <p>
 *
 * </p>
 *
 * @author: wfs
 * @date: 2021/6/21
 */
public class Student {
    private  static  int age;//静态的变量  多线程
    private  double score;//非静态
    public void run(){

    }
    public static void go(){

    }

    public static void main(String[] args) {
        System.out.println(Student.age);
        //System.out.println(Student.score);没有static修饰
        Student s1=new Student();
        System.out.println(s1.age);
        System.out.println(s1.score);

    }
}
package com.oop.demo05;

/**
 * <p>
 *
 * </p>
 *
 * @author: wfs
 * @date: 2021/6/21
 */
public class Student {
    private  static  int age;//静态的变量  多线程
    private  double score;//非静态
    public void run(){
        go();//非静态可以访问静态方法所有东西
    }
    public static void go(){
        //静态方法可以调用静态方法,不可以调用非静态方法

    }

    public static void main(String[] args) {
        new Student().run();
        Student.go();
        go();//非静态的方法可以访问main
        //run();//静态方法可以调用静态方法,不可以调用非静态方法
    }
}

静态代码块 &非静态代码块

package com.oop.demo05;

/**
 * <p>
 *
 * </p>
 *
 * @author: wfs
 * @date: 2021/6/21
 */
public class Person {
    {
        System.out.println("匿名代码块");
    }
    static {//只执行一次
        System.out.println("静态代码块");
    }
    Person(){
        System.out.println("无参构造");
    }

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

    }


}

执行的结果

静态代码块 匿名代码块 无参构造

 public static void main(String[] args) {
        Person person = new Person();
        System.out.println("============");
        Person person1 = new Person();

    }

 

静态代码块
匿名代码块
无参构造
============
匿名代码块
无参构造
package com.oop.demo05;
import static java.lang.Math.random;
//导入静态代码包
/**
 * <p>
 *
 * </p>
 *
 * @author: wfs
 * @date: 2021/6/21
 */
public class Test {
    public static void main(String[] args) {
        System.out.println(random());

    }
}

  

 

static关键字

原文:https://www.cnblogs.com/swfsa/p/14912507.html

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