Day8-4 static关键字详解

static关键字详解

 package com.oop.demo07;
 ​
 //static
 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();
 ​
         System.out.println(Student.age);
         System.out.println(s1.age);
         System.out.println(s1.score);
     }
 }

 

 
package com.oop.demo07;
 ​
 public class Person {
     /*
     {
         //代码块(匿名代码块)
     }
 ​
     static {
         //静态代码块
     }
      */
     //第二个执行 赋初值
     {
         System.out.println("匿名代码块");
     }
     //第一个执行,只执行一次
     static {
         System.out.println("静态代码块");
     }
     //第三个执行
     public Person() {
         System.out.println("构造方法");
     }
 ​
     public static void main(String[] args) {
         Person person1 = new Person();
         System.out.println("==================");
         Person person2 = new Person();
     }
 }

 

 package com.oop.demo07;
 //静态导入包
 import static  java.lang.Math.random;
 public class Test {
     public static void main(String[] args) {
         System.out.println(Math.random());
     }
 }

 

 
posted @ 2022-11-18 14:35  actadams68  阅读(34)  评论(0)    收藏  举报