Learn Java Tutorial for Beginners (Video), Part 19: Static (and Final)

A tutorial on the static keyword in Java. The first part of the tutorial covers the basic meaning of static; after that we look at what you can do with static, including counting objects, assigning IDs to objects automatically, and defining constants that are associated with your class.

When the video is running, click the maximize button in the lower-right-hand corner to make it full screen.



Code for this tutorial:

 

class Thing {
    public final static int LUCKY_NUMBER = 7;
    
    public String name;
    public static String description;
    
    public static int count = 0;
    
    public int id;
    
    public Thing() {
        
        id = count;
        
        count++;
    }
    
    public void showName() {
        System.out.println("Object id: " + id + ", " + description + ": " + name);
    }
    
    public static void showInfo() {
        System.out.println(description);
        // Won't work: System.out.println(name);
    }
}


public class App {

    public static void main(String[] args) {
        
        Thing.description = "I am a thing";
        
        Thing.showInfo();
        
        System.out.println("Before creating objects, count is: " + Thing.count);
        
        Thing thing1 = new Thing();
        Thing thing2 = new Thing();
        
        System.out.println("After creating objects, count is: " + Thing.count);
        
        thing1.name = "Bob";
        thing2.name = "Sue";
        
        thing1.showName();
        thing2.showName();
        
        System.out.println(Math.PI);
        
        System.out.println(Thing.LUCKY_NUMBER);
    }

}

 
I am a thing
Before creating objects, count is: 0
After creating objects, count is: 2
Object id: 0, I am a thing: Bob
Object id: 1, I am a thing: Sue
3.141592653589793
7