为 static 变量分配空间,设置默认值(准备阶段)

话不多说直接上代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package com.beyond.dhl;
 
 
class Singleton {
    private static Singleton instance;   // 懒汉式所以不会进行实例化对象
 
    private Singleton() {
        System.out.println("构造方法:" + Thread.currentThread().getName());
    }
 
    public static Singleton getInstance() {
        if (instance == null) {
            instance = new Singleton();
        }
        return instance;
    }
 
 
}
 
public class Test {
    public static void main(String[] args) {
        new Thread(() -> Singleton.getInstance(), "线程A").start();
        new Thread(() -> Singleton.getInstance(), "线程B").start();
        new Thread(() -> Singleton.getInstance(), "线程C").start();
        new Thread(() -> Singleton.getInstance(), "线程D").start();
        new Thread(() -> Singleton.getInstance(), "线程E").start();
    }
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
public class Demo01 {
    public static void main(String[] args) {
        int i = 10//十进制
        int i2 = 010//八进制0
        int i3 = 0x10//十六进制0x
        int i4 = 0b10;  //二进制0b
 
        System.out.println(i);
        System.out.println(i2);
        System.out.println(i3);
        System.out.println(i4);
    }
}
 
 
public class Demo02 {
    public static void main(String[] args) {
        float f = 0.1f;
        double d = 1.0/10;
 
        System.out.println(f == d);
        System.out.println(f);
        System.out.println(d);
 
        System.out.println("========================================");
 
        float d1 = 233333333333333333F;
        float d2 = d1 + 1;
        System.out.println(d1 == d2);
    }
}
 
public class MyClass {
    static int num;
}
public class Test_MyClass {
    public static void main(String[] args) {
        //可直接通过  类名.属性名  进行访问
        MyClass.num=10;
         
        MyClass m1 = new MyClass();
        m1.num=20;
        System.out.println(m1.num);//num=20
         
        MyClass m2 = new MyClass();
        m2.num=30;
        System.out.println(m2.num);//num=30
        System.out.println(m1.num);//num=30
    }
}
 
 
package com.soder;
public class Soder1 extends Soders{
     
    public Soder1() {
        p--;
        System.out.println("子类构造方法\t"+p);
    }
    {
        System.out.println("子类代码块\t"+p);
    }
     
    static {
        System.out.println("子类静态代码块\t"+p);
    }
    public static void m1() {
        System.out.println("子类静态指令m1\t"+p);
    }
    public void m2() {
        System.out.println("子类指令m2\t"+p);
    }
}

tips:当类被加载时,类的所有信息(包名,类名,属性,方法等)都会被加载到方法区,而其中的静态属性方法又会被调进静态域,可通过类名直接调用出来

posted @   Java团长  阅读(523)  评论(0)    收藏  举报
编辑推荐:
· 长文讲解 MCP 和案例实战
· Hangfire Redis 实现秒级定时任务,使用 CQRS 实现动态执行代码
· Android编译时动态插入代码原理与实践
· 解锁.NET 9性能优化黑科技:从内存管理到Web性能的最全指南
· 通过一个DEMO理解MCP(模型上下文协议)的生命周期
阅读排行:
· 工良出品 | 长文讲解 MCP 和案例实战
· 多年后再做Web开发,AI帮大忙
· 记一次 .NET某旅行社酒店管理系统 卡死分析
· centos停服,迁移centos7.3系统到新搭建的openEuler
· 上周热点回顾(4.14-4.20)
点击右上角即可分享
微信分享提示