QQ登录

只需要一步,快速开始

 注册地址  找回密码
查看: 5994|回复: 0
打印 上一主题 下一主题

[推荐] Java 静态方法static和非静态方法的区别.static 详解

[复制链接]
字体大小: 正常 放大
杨利霞        

5250

主题

81

听众

16万

积分

  • TA的每日心情
    开心
    2021-8-11 17:59
  • 签到天数: 17 天

    [LV.4]偶尔看看III

    网络挑战赛参赛者

    网络挑战赛参赛者

    自我介绍
    本人女,毕业于内蒙古科技大学,担任文职专业,毕业专业英语。

    群组2018美赛大象算法课程

    群组2018美赛护航培训课程

    群组2019年 数学中国站长建

    群组2019年数据分析师课程

    群组2018年大象老师国赛优

    跳转到指定楼层
    1#
    发表于 2020-4-19 16:16 |只看该作者 |倒序浏览
    |招呼Ta 关注Ta
    Java 静态方法static和非静态方法的区别.static 详解
    一、静态方法可以不用创建类的对象直接使用(需要导入类)

    Student.java

    package opp.staticFunction;

    public class Student {
        public static String say (){
            System.out.println("static :Student class function say()");
            return "static :Student class function say()";
        }
    }
    1
    2
    3
    4
    5
    6
    7
    8
    Demo.java

    (导入 Student文件后不用创建类的对象直接使用)

    package opp.staticFunction;
    import static opp.staticFunction.Student.say;

    public class Demo {
        public static void main(String[] args) {
            say();
        }
    }
    1
    2
    3
    4
    5
    6
    7
    8
    运行结果

    static :Student class function say()
    1
    二、非静态方法需要创建类的对象,然后对象再使用对应方法才能使用(依旧要导入类)

    Student.java

    package opp.staticFunction;

    public class Student {
        public  String say (){
            System.out.println("static :Student class function say()");
            return "static :Student class function say()";
        }
    }
    1
    2
    3
    4
    5
    6
    7
    8
    Demo.java

    package opp.staticFunction;
    public class Demo {
        public static void main(String[] args) {
            Student student = new Student();
            student.say();
        }
    }

    1
    2
    3
    4
    5
    6
    7
    8
    输出结果

    static :Student class function say()
    1
    =======================================================

    三、静态属性

    package opp.staticFunction静态方法;

    public class 静态属性 {
        private static String name;
        private int age;
        public static void main(String[] args) {
            //static 修饰的属性的变量不用new 就能访问,推荐使用 静态属性.name(类名.变量名);来区分静态变量(类变量)
            System.out.println(静态属性.name);//null
            //System.out.println(静态属性.age);非静态变量,编译报错需要New 来访问.
            静态属性 obj = new 静态属性();
            System.out.println(obj.age);//0
            System.out.println(obj.name);//可以访问但是不推荐使用。使用类变量方法来区分.

        }
    }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    四、静态代码块和代码块.

    1.静态代码块 代码块 构造方法的执行顺序:

    静态代码块 – 代码块 – 构造方法

    package opp.staticFunction静态方法;

    /**
    * 用于测试 静态代码块  代码块  构造函数 在 new 时候的执行顺序.
    */
    public class StaticCodeBlock {
        {
            System.out.println("代码块执行成功!");
        }
        static {
            System.out.println("静态代码块执行成功!");
        }
        StaticCodeBlock(){
            System.out.println("structure method run successfully.构造方法运行成功");
        }

        public static void main(String[] args) {
            new StaticCodeBlock();
            System.out.println("=========  2次 new =============");
            new StaticCodeBlock();
        }
    }

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    结果

    静态代码块执行成功!
    代码块执行成功!
    structure method run successfully.构造方法运行成功
    =========  2次 new =============
    代码块执行成功!
    structure method run successfully.构造方法运行成功
    1
    2
    3
    4
    5
    6
    2.静态代码块只执行一次(因为静态static 在类同时加载,所以只加载一次)



    拓展:静态导入包

    导入包前使用 Math.random() 函数代码

    package opp.staticFunction静态方法;

    public class Random {
        public static void main(String[] args) {
            System.out.println(Math.random());
        }
    }
    1
    2
    3
    4
    5
    6
    7
    导入包

    import static java.lang.Math.random();
    1
    导入包后 可以直接使用 random() 函数

    package opp.staticFunction静态方法;
    //导入包
    import static java.lang.Math.random
    public class Random {
        public static void main(String[] args) {
            System.out.println(Math.random());
            //导入包后
            System.out.println(random());
        }
    }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    拓展2 final类无法被继承.
    ————————————————
    版权声明:本文为CSDN博主「半亩方糖里」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/jarvan5/article/details/105472572

    zan
    转播转播0 分享淘帖0 分享分享0 收藏收藏0 支持支持0 反对反对0 微信微信
    您需要登录后才可以回帖 登录 | 注册地址

    qq
    收缩
    • 电话咨询

    • 04714969085
    fastpost

    关于我们| 联系我们| 诚征英才| 对外合作| 产品服务| QQ

    手机版|Archiver| |繁體中文 手机客户端  

    蒙公网安备 15010502000194号

    Powered by Discuz! X2.5   © 2001-2013 数学建模网-数学中国 ( 蒙ICP备14002410号-3 蒙BBS备-0002号 )     论坛法律顾问:王兆丰

    GMT+8, 2024-4-24 11:09 , Processed in 0.393242 second(s), 50 queries .

    回顶部