Java-static关键字
生活随笔
收集整理的這篇文章主要介紹了
Java-static关键字
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
對于屬性和方法
public class Student {private static int age; // 靜態(tài)的變量 共享這一個數(shù)據(jù) 多線程用的多private double score; // 非靜態(tài)的變量public void run(){go();}public static void go(){}public static void main(String[] args) {Student s1 = new Student();System.out.println(Student.age); // System.out.println(Student.score); // Non-static field 'score' cannot be referenced from a static contextSystem.out.println(s1.age);System.out.println(s1.score);new Student().run();Student.go();go();} }靜態(tài)代碼塊
public class Person {// 2 可以用來 賦初始值{// 代碼塊 (匿名代碼塊)System.out.println("匿名代碼塊");}// 1static {// 靜態(tài)代碼塊 類加載時執(zhí)行,永久只執(zhí)行一次System.out.println("靜態(tài)代碼塊");}// 3public Person() {System.out.println("構(gòu)造方法");}public static void main(String[] args) {Person person = new Person();System.out.println("=================");Person person1 = new Person();} }final關(guān)鍵字修飾的類不能被繼承
https://www.bilibili.com/video/BV12J41137hu?p=73
總結(jié)
以上是生活随笔為你收集整理的Java-static关键字的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux-centos7 关机命令、系
- 下一篇: Java-break-continue