Java static-静态方法、非静态方法、各种方法的执行顺序、spring注入遇到静态方法/变量的情况

static可以修饰方法、变量和代码段
被static修饰的方法和变量就可以直接通过函数名调用,不需要实例化,而且这些被修饰的方法和变量只有一个,被某对象修改后,其他对象再调用会是修改后的结果。
静态的只可以使用静态的,非静态的可以使用非静态的和静态的

静态方法

public class A {
    public static int a;
    public static void setA(int m){
        a=m;
    }
    public static int getA(){
        return a;
    }
}
public class B {
    public int lll(){
        return A.getA();
    }
}
public class C {
    public static void main(String[] args) {
        A.setA(10);
        System.out.println(new B().lll());
    }
}

结果

10

非静态方法

表示方法

{
代码
}

各种方法的执行顺序

学习自
https://blog.csdn.net/qq_35868412/article/details/89360250
https://blog.csdn.net/qq_34111779/article/details/78160036

从main函数开始运行的话

  1. 引入类过程:static方法(一个类只会被引入一次)
  2. 实例化过程:非静态方法、构造方法(每new一次就会实例化一次)

例子:

public class Shape {
    public String name = "shape";
    public static String staticName="staticshap";

    static {
        System.out.println("Shape 的静态代码块---------");
        System.out.println(staticName);
        staticName="staticshap2";
        System.out.println(staticName);
    }

    {
        System.out.println("shape的非静态方法");
    }

    public Shape(){
        System.out.println("shape constructor");
    }

    public void printType() {
        System.out.println("this is shape");
    }

    public static void printName() {
        System.out.println("shape");
    }


}
public class Circle extends Shape{
    public String name = "circle";
    public static String staticName="staticcircle";

    static {
        System.out.println("circle 的静态代码块---------");
        System.out.println(staticName);
        staticName="staticcircle2";
        System.out.println(staticName);
    }

    {
        System.out.println("circle 的非静态方法");
    }

    public Circle() {
        System.out.println("circle constructor");
    }

    public void printType() {
        System.out.println("this is circle");
    }

    public static void printName() {
        System.out.println("circle");
    }
    public static void main(String[] args) {
        System.out.println("main方法---------");
        Shape shape = new Circle();
        System.out.println(shape.name);
        System.out.println(shape.staticName);
        shape.printType();
        shape.printName();
        Shape shape2 = new Shape();
        System.out.println(shape2.name);
        System.out.println(shape.staticName);
        shape2.printType();
        shape2.printName();
        Circle circle = new Circle();
        System.out.println(circle.name);
        System.out.println(circle.staticName);
        circle.printType();
        circle.printName();

    }
}

结果

Shape 的静态代码块---------
staticshap
staticshap2
circle 的静态代码块---------
staticcircle
staticcircle2
main方法---------
shape的非静态方法
shape constructor
circle 的非静态方法
circle constructor
shape
staticshap2
this is circle
shape
shape的非静态方法
shape constructor
shape
staticshap2
this is shape
shape
shape的非静态方法
shape constructor
circle 的非静态方法
circle constructor
circle
staticcircle2
this is circle
circle

静态变量、方法被spring bean依赖注入

entity的静态变量只可以被依赖注入一次,如果多次则取最后一次注入的结果。
get、set方法可以需要是非静态,我测试的时候使用static的get,set方法,依然可以得到和非静态一样的结果。但是编译器会显示没有set方法

public class School {
    private static int id;

    public  int getId() {
        return id;
    }

    public static void setId(int a) {
        id = a;
    }
}
<bean id="school1" class="entity.School">
        <property name="id" value="1"></property>
    </bean>
    <bean id="school2" class="entity.School">
        <property name="id" value="2"></property>
    </bean>
ApplicationContext applicationContext =new ClassPathXmlApplicationContext("spring.xml");

School anew2 =(School) applicationContext.getBean("school2");
System.out.println(anew2.getId());
School anew =(School) applicationContext.getBean("school1");
System.out.println(anew.getId());

结果

2
2

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值