Static关键字详解,你学会了吗?

线程对象通过start方法进入runnable状态,启动的线程不一定会立即得到执行,线程的运行与否要
首页 新闻资讯 行业资讯 Static关键字详解,你学会了吗?

static关键字详解

复制

public class Person {//2:赋初始值~{System.out.println("匿名代码块");}//1: 只执行一次~static {System.out.println("静态代码块");}//3public Person() {System.out.println("构造方法");}public static void main(String[] args) {Person p1 = new Person();System.out.println("================");Person p2 = new Person();}}//输出结果
静态代码块
匿名代码块
构造方法================匿名代码块
构造方法

Process finished with exit code 0
  • 1.

  • 2.

  • 3.

  • 4.

  • 5.

  • 6.

  • 7.

  • 8.

  • 9.

  • 10.

  • 11.

  • 12.

  • 13.

  • 14.

  • 15.

  • 16.

  • 17.

  • 18.

  • 19.

  • 20.

  • 21.

  • 22.

  • 23.

  • 24.

  • 25.

  • 26.

  • 27.

  • 28.

  • 29.

  • 30.

  • 31.

  • 32.

  • 33.

复制

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

    public void run(){}public static void go(){}public static void main(String[] args) {new Student().run();Student.go();go();}}
  • 1.

  • 2.

  • 3.

  • 4.

  • 5.

  • 6.

  • 7.

  • 8.

  • 9.

  • 10.

  • 11.

  • 12.

  • 13.

  • 14.

  • 15.

  • 16.

  • 17.

  • 18.

  • 19.

复制

//静态导入包
import static java.lang.Math.random;import static java.lang.Math.PI;public class Test1 {public static void main(String[] args) {System.out.println(random());System.out.println(PI);}}
  • 1.

  • 2.

  • 3.

  • 4.

  • 5.

  • 6.

  • 7.

  • 8.

  • 9.

  • 10.

每日Java面试题

1.线程NEW状态

new创建一个Thread对象时,并没处于执行状态,因为没有调用start方法启动改线程,那么此时的状态就是新建状态。

2.线程RUNNABLE状态

线程对象通过start方法进入runnable状态,启动的线程不一定会立即得到执行,线程的运行与否要看cpu的调度,我们把这个中间状态叫可执行状态(RUNNABLE)。

3.线程的RUNNING状态

一旦cpu通过轮询或其他方式从任务可以执行队列中选中了线程,此时它才能真正的执行自己的逻辑代码。

11    2023-03-09 07:38:58    static 关键字 状态