Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Static or Class attribute

public class Bike {
    private static int count = 0;
    public static int getCount() {
        return Bike.count;
    }
    public Bike() {
        Bike.count++;
    }
    public void finalize() {
        Bike.count--;
        System.out.println("Finalize");
    }
}
public class RunBike {
    public static void bike_shop() {
        System.out.println(Bike.getCount());
        Bike a = new Bike();
        System.out.println(Bike.getCount());
        Bike b = new Bike();
        System.out.println(Bike.getCount());
        Bike c = new Bike();
        System.out.println(Bike.getCount());
        b = null;
        //System.gc();
        System.out.println(Bike.getCount());
    }
    public static void main(String[] args) {
        bike_shop();
        System.out.println(Bike.getCount());
    }
}

// The finalize methods is supposed to be similar to a destructor, but it might be never called in the life of the program. So we cannot rely on it.