关于java中static一个有意思的小案例

package com.example;

class Employee {
    private static Employee employee = new Employee();
    public static int count1 = 0;
    public static int count2;

    private Employee(){
        count1++;
        count2++;
    };

    public static Employee getInstance(){
        return  employee;
    }
}

public class TestLoader{
    public static void main(String[] args){
        Employee employee = Employee.getInstance();
        System.out.println("count1-------"+employee.count1);
        System.out.println("count2-------"+employee.count2);
    }
}

结果为:

count1-------0
count2-------1

相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页