Java-Use of Static

Use of Static

Static variables are used when we want to have a variable common to all instances of a class. One of the most common examples is to have a variable that could keep a count of how many objects of a class have been created. Remember, Java creates only one copy for a static variable which can be used even if the class is never actually instantiated.



Program Source

class Data {

    static int count = 0;


public class Javaapp {

    public static void main(String[] args) {
        Data d1 = new Data();
        Data d2 = new Data();
        Data d3 = new Data();
        System.out.println("Totel Objects : "+Data.count);

Leave a Comment