static

Posted on 2023-03-26 23:10  离001  阅读(22)  评论(0编辑  收藏  举报

static

 

加在方法上面叫静态方法,加在属性上面叫静态属性。

静态变量是和类加载的

静态变量

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

   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);

  }
}

Student.age.sout 输出

静态方法

public class Student extends Person{
   
   
   //静态随着类的加载就存在了

   public void run(){
       go();//非静态方法可以直接调用静态方法
  }
   public static void go(){

  }

   public static void main(String[] args) {
       new Student().run();//调用非静态方法比较麻烦
       Student.go();//静态方法可以在这个类中直接调用
       go();//直接调用
       
  }
}

 

代码块

public class Person {

  {
       //匿名代码块
  }

   static{
       //静态代码块
  }
}

 

代码块详细

public class Person {

   //第二个输出,一般用匿名代码块赋初始值
  {
       System.out.println("匿名代码块");
  }

   //第一个输出,优先级最高,只执行一次
   static{
       System.out.println("静态代码块");
  }


   //第三个输出 这个是构造器;
   //快捷键:Alt insert + Constructor
   public Person() {
       System.out.println("构造方法");
  }

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

  }
}

静态导入包

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

//还可以导入π
import static java.lang.Math.PI;//导入PI,当然很少人会这么写


public class Test {
   public static void main(String[] args) {
       
       System.out.println(Math.random());//导入静态包就可以直接调用,不用写Math了
       System.out.println(PI);
  }
}
//final断子绝孙,是常量修饰符

 

 

 

 

static

 

加在方法上面叫静态方法,加在属性上面叫静态属性。

静态变量是和类加载的

静态变量

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

   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);

  }
}

Student.age.sout 输出

静态方法

public class Student extends Person{
   
   
   //静态随着类的加载就存在了

   public void run(){
       go();//非静态方法可以直接调用静态方法
  }
   public static void go(){

  }

   public static void main(String[] args) {
       new Student().run();//调用非静态方法比较麻烦
       Student.go();//静态方法可以在这个类中直接调用
       go();//直接调用
       
  }
}

 

代码块

public class Person {

  {
       //匿名代码块
  }

   static{
       //静态代码块
  }
}

 

代码块详细

public class Person {

   //第二个输出,一般用匿名代码块赋初始值
  {
       System.out.println("匿名代码块");
  }

   //第一个输出,优先级最高,只执行一次
   static{
       System.out.println("静态代码块");
  }


   //第三个输出 这个是构造器;
   //快捷键:Alt insert + Constructor
   public Person() {
       System.out.println("构造方法");
  }

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

  }
}

静态导入包

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

//还可以导入π
import static java.lang.Math.PI;//导入PI,当然很少人会这么写


public class Test {
   public static void main(String[] args) {
       
       System.out.println(Math.random());//导入静态包就可以直接调用,不用写Math了
       System.out.println(PI);
  }
}
//final断子绝孙,是常量修饰符

快捷方式

输出内容+ .sout