数学建模社区-数学中国

标题: Java 静态方法static和非静态方法的区别.static 详解 [打印本页]

作者: 杨利霞    时间: 2020-4-19 16:16
标题: Java 静态方法static和非静态方法的区别.static 详解
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






欢迎光临 数学建模社区-数学中国 (http://www.madio.net/) Powered by Discuz! X2.5