Java static member variable example - Java Object Oriented Design

Java examples for Object Oriented Design:Static

Description

Java static member variable example

Demo Code


public class Main {
       //w  ww.  j a va  2  s. co  m
        public static void main(String[] args) {
               
                ObjectCounter object1 = new ObjectCounter();
                System.out.println(object1.getNumberOfObjects());
               
                ObjectCounter object2 = new ObjectCounter();
                System.out.println(object2.getNumberOfObjects());
                               
        }
}
 
class ObjectCounter{
        static int counter=0;
       
        public ObjectCounter(){
                counter++;
        }
        public int getNumberOfObjects(){
                return counter;
        }
}

Result


Related Tutorials