class Student { private static int count=0; //static variable Student() { count++; //increment static variable } static void showCount() //static method { System.out.println(“Number Of students : “+count); } } public class StaticDemo { public static void main(String[] args) { Student s1=new Student(); Student s2=new Student(); Student.showCount(); //calling static method Student s3=new Student(); Student s4=new Student(); s4.showCount(); //calling static method } }
Output: Number of students : 2 Number of students : 4Explanation: