1.java中静态方法调用非静态成员变量、非静态方法
- public class Circle {
- private double radius = 1.0;
- double getAre() {
- return radius * radius * Math.PI;
- }
- public static void main(String[] args) {
- Circle myCircle = new Circle();
- System.out.println("radius is " + myCircle.radius); //radius不是静态的,静态方法可以通过类对象调用非静态成员变量、非静态方法
- }
- }
2.不new对象,只能访问静态成员变量。而静态成员变量注入只能使用set注入,原因是静态变量/类变量不是对象的属性,而是一个类的属性,spring则是基于对象层面上的依赖注入.
private static MqttServerProxy mProxy;
private static DeviceStateService devStateService;
public static void main(String[] args) {
SpringApplication.run(App.class, args);
//启动,自动连接mqtt
LOGGER.info("APP: run().start to connect mqtt...");
devStateService.setMqttProxy(mProxy);
devStateService.startService();
}
@Autowired
public void setMqttServerProxy(MqttServerProxy proxy) {
mProxy = proxy;
}
@Autowired
public void setDeviceStateService(DeviceStateService service) {
devStateService = service;
}