我的日常开发记录日志
首页
  • Laravel
  • Thinkphp
  • Swoole
  • Workman
  • php
  • HTML
  • CSS
  • JavaScript
  • Vue
  • ES6
  • 小程序
  • Mysql
  • Redis
  • Es
  • MongoDb
  • Git
  • Composer
  • Linux
  • Nginx
  • Docker
  • Vpn
  • 开发实战
  • 开发工具类
  • 友情链接
💖关于
💻收藏
  • 分类
  • 标签
  • 归档数据
GitHub (opens new window)

我的日常开发记录日志

never give up
首页
  • Laravel
  • Thinkphp
  • Swoole
  • Workman
  • php
  • HTML
  • CSS
  • JavaScript
  • Vue
  • ES6
  • 小程序
  • Mysql
  • Redis
  • Es
  • MongoDb
  • Git
  • Composer
  • Linux
  • Nginx
  • Docker
  • Vpn
  • 开发实战
  • 开发工具类
  • 友情链接
💖关于
💻收藏
  • 分类
  • 标签
  • 归档数据
GitHub (opens new window)
  • laravel

  • thinkphp

  • swoole

  • workman

  • php

  • gpt

  • java

    • map和list遍历
    • 基本的类
    • 静态方法和属性
      • java包相关
      • 基础mvc结构
      • 注解理解
      • 泛型
      • 数据库查询
    • 后端
    • java
    窝窝侠
    2024-08-06

    静态方法和属性

    在 Java 中,static 方法是属于类本身的,而不是类的实例。因此,static 方法可以通过类名直接调用,而不需要创建类的实例。下面是 static 方法调用的详细信息:

    # 如何调用 static 方法

    1. 通过类名调用:

      • 直接使用类名来调用 static 方法,这是最常见和推荐的方式。这样可以清楚地表明方法是类级别的,而不是实例级别的。
      • 示例:
        public class MyClass {
            public static void staticMethod() {
                System.out.println("This is a static method.");
            }
        }
        
        public class Test {
            public static void main(String[] args) {
                // 通过类名调用 static 方法
                MyClass.staticMethod();
            }
        }
        
        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        11
        12
    2. 通过类的实例调用 (不推荐):

      • 尽管可以通过类的实例调用 static 方法,Java 编译器允许这样做,但这种做法不推荐,因为它可能会导致代码的误解和不清晰。
      • 示例:
        public class MyClass {
            public static void staticMethod() {
                System.out.println("This is a static method.");
            }
        }
        
        public class Test {
            public static void main(String[] args) {
                // 通过实例调用 static 方法(不推荐)
                MyClass obj = new MyClass();
                obj.staticMethod(); // 这种调用方式仍然有效,但不推荐
            }
        }
        
        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        11
        12
        13

    # 注意事项

    • 静态方法不能访问实例变量和实例方法:

      • static 方法只能访问静态变量和调用其他静态方法,因为它们与实例无关。
      • 示例:
        public class MyClass {
            private static int staticVar = 10;
            private int instanceVar = 20;
        
            public static void staticMethod() {
                // 可以访问静态变量
                System.out.println("Static variable: " + staticVar);
                
                // 不能访问实例变量
                // System.out.println("Instance variable: " + instanceVar); // 编译错误
        
                // 可以调用其他静态方法
                otherStaticMethod();
            }
        
            public static void otherStaticMethod() {
                System.out.println("This is another static method.");
            }
        }
        
        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        11
        12
        13
        14
        15
        16
        17
        18
        19
    • 静态方法可以被继承,但不能被重写:

      • 你可以在子类中定义一个与父类 static 方法同名的方法,但这并不是重写,而是方法隐藏(hiding)。
      • 示例:
        public class Parent {
            public static void staticMethod() {
                System.out.println("Parent static method.");
            }
        }
        
        public class Child extends Parent {
            public static void staticMethod() {
                System.out.println("Child static method.");
            }
        }
        
        public class Test {
            public static void main(String[] args) {
                Parent.staticMethod(); // 输出: Parent static method.
                Child.staticMethod();  // 输出: Child static method.
            }
        }
        
        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        11
        12
        13
        14
        15
        16
        17
        18

    # 总结

    • 推荐: 通过类名直接调用 static 方法。
    • 不推荐: 通过类实例调用 static 方法。
    • 静态方法限制: 不能访问实例变量和方法,只能访问静态变量和方法。
    在线编辑 (opens new window)
    上次更新: 2025/02/25, 18:30:54
    基本的类
    java包相关

    ← 基本的类 java包相关→

    最近更新
    01
    showprocess用法
    04-29
    02
    vue3中尖括号和冒号的使用细则
    04-29
    03
    sd使用
    02-22
    更多文章>
    🖥️

    © 2025窝窝侠 💌 豫ICP备20005263号-2 🛀 Theme by 💝 Vdoing && 小胖墩er

    • 跟随系统
    • 浅色模式
    • 深色模式
    • 阅读模式
    ×