Static Method Example

This program demonstrates static method usage.

Problem Statement

Write a Java program to define static method showCount() in Student class.

Source Code

1 class Student {
2 static int count=0;
3 Student(){ count++; }
4 static void showCount(){
5 System.out.println("Total students: "+count);
6 }
7 }
8 public class StaticMethod {
9 public static void main(String[] args) {
10 System.out.println("Eduinq Static Method");
11 Student s1=new Student();
12 Student s2=new Student();
13 Student.showCount();
14 }
15 }

Program Output

Eduinq Static Method
Total students: 2

Explanation

Static methods can be called without creating an object. - showCount() directly accesses static variable count. - This avoids needing an object reference to call the method. - Static methods are useful for utility functions or operations that depend only on class level data, not individual object state. Conclusion: Shows how to define and call static methods.

Quick Links to Explore