• static 关键字


           

    目录

    一、static 修饰变量

    二、static 修饰方法

    三、static 修饰代码块

    四、static 修饰内部类

    五、静态数据初始化顺序


            static 理解为静态的,在 Java 中,static 关键字可以使用在变量、方法、代码块、内部类等,类被加载就初始化。static 关键字属于类,而不是实例。

    一、static 修饰变量

            static 修改变量称为静态变量。

    示例

    1. public class Person {
    2. String name;
    3. int age;
    4. static String city = "深圳"; // 静态变量,属于类,所有对象共享。
    5. public Person(String name, int age) {
    6. super();
    7. this.name = name;
    8. this.age = age;
    9. }
    10. public void talk() {
    11. System.out.println("我是:"+this.name + " 年龄:"+this.age+" 来自:"+city);
    12. }
    13. public static void main(String[] args) {
    14. Person p1 = new Person("张三",18);
    15. Person p2 = new Person("李四",19);
    16. Person p3 = new Person("王五",20);
    17. p1.talk();
    18. p2.talk();
    19. p3.talk();
    20. }
    21. }

            假设程序产生了1000个Person对象,如果需要修改所有人 city 属性值,如果city属性值没有被static修饰,那是需要重新修改1000次 city 属性值,从开发以及内存管理来看,加了static话,像需要创建比较多实例对象并且对象有共同属性以及属性值也相同的情况下,可以用 static 修饰是可以适当提供内存效率的。

           static 类型变量是所有对象共享的内存空间,也就是无论多少个对象产生,也只有一个 static 类型的属性。

            示例内存图如下:

    二、static 修饰方法

             static 修改方法称为静态方法。静态方法无需创建对象就可以直接使用。

    1. public class Person {
    2. String name;
    3. static String city = "深圳";
    4. public void say(String name) {
    5. this.name = name;
    6. }
    7. public static void talk() {
    8. //say("张三");//静态方法不能调用非静态方法,编译不通过
    9. //this. 不能使用this关键字
    10. //super.不能使用super关键字
    11. System.out.println("我来自:"+city);
    12. }
    13. public static void main(String[] args) {
    14. talk(); //没有实例化对象,talk方法就可以直接使用
    15. }
    16. }

            1、静态方法不能调用非静态的方法和变量。

            2、不能使用this和super关键字

    三、static 修饰代码块

             static 修改代码块称为静态代码块。静态代码块,是 Java 类中的 static{ } 修饰的代码。用于类初始化时,为类的静态变量赋初始值,提升程序性能。

    1. public class Person {
    2. static { // 静态代码块
    3. }
    4. }

    四、static 修饰内部类

            static 修改内部类称为静态内部类。

            在创建静态内部类的实例时,不需要创建外部类的实例。

    1. public class Outer {
    2. static class Inner {
    3. }
    4. }
    5. class OtherClass {
    6. Outer.Inner inner = new Outer.Inner();//在创建静态内部类的实例时,不需要创建外部类的实例
    7. }

    五、静态数据初始化顺序

            示例

    1. class Bowl{
    2. Bowl(int marker){
    3. System.out.println("Bowl("+marker+")");
    4. }
    5. void f1(int marker) {
    6. System.out.println("f1("+marker+")");
    7. }
    8. }
    9. class Tbable{
    10. static Bowl bowl= new Bowl(1);
    11. Tbable(){
    12. System.out.println("Tbable()");
    13. }
    14. void f2(int marker) {
    15. System.out.println("f2("+marker+")");
    16. }
    17. static Bowl bowl2= new Bowl(2);
    18. }
    19. class Cupboard{
    20. Bowl bow3= new Bowl(3);
    21. static Bowl bow4= new Bowl(4);
    22. Cupboard(){
    23. System.out.println("Cupboard()");
    24. bow4.f1(2);
    25. }
    26. void f3(int marker) {
    27. System.out.println("f3("+marker+")");
    28. }
    29. static Bowl bowl5= new Bowl(5);
    30. }
    31. public class StaticInitalization {
    32. public static void main(String[] args) {
    33. System.out.println("create new Cupboard() in main");
    34. new Cupboard();
    35. System.out.println("create new Cupboard() in main");
    36. new Cupboard();
    37. tbable.f2(1);
    38. cupboard.f3(1);
    39. }
    40. static Tbable tbable = new Tbable();
    41. static Cupboard cupboard = new Cupboard();
    42. }

    控制台输出

    1. Bowl(1)
    2. Bowl(2)
    3. Tbable()
    4. Bowl(4)
    5. Bowl(5)
    6. Bowl(3)
    7. Cupboard()
    8. f1(2)
    9. create new Cupboard() in main
    10. Bowl(3)
    11. Cupboard()
    12. f1(2)
    13. create new Cupboard() in main
    14. Bowl(3)
    15. Cupboard()
    16. f1(2)
    17. f2(1)
    18. f3(1)

    由示例可知:初始化对象是先静态对象。

  • 相关阅读:
    南大通用GBase8s 常用SQL语句(257)
    “智能与未来”2024世亚国际智能机器人展会(简称:世亚智博会)
    python——上下文管理器with
    ASUS华硕冰刃7笔记本GX650PY原装Windows11工厂安装包 恢复出厂开箱状态预装OEM系统 带MyASUS in WinRE重置功能
    电路第一章 电路变量
    大数据课程L4——网站流量项目的Hive离线批处理
    分词算法----正向和逆向最大匹配算法(含Python代码实现)
    【力扣】16. 最接近的三数之和
    Go 项目依赖注入wire工具最佳实践介绍与使用
    C++ primer 查漏补缺九:第六章 函数
  • 原文地址:https://blog.csdn.net/u012965203/article/details/127720901