Java Program to Explain Static Class

You are currently viewing Java Program to Explain Static Class

Java program to explain static class

//Java program to demonstrate example of static class.

import java.util.*;

public class StaticClassExample {
    static int a = 0;

    //static class declaration
    static class ClsInner {
        //method of static class
        public void dispMessage() {
            a = 20;
            System.out.println("Value of a: " + a);
        }
    }

    //main()
    public static void main(String[] s) {
        //create object of static inner class
        StaticClassExample.ClsInner objClsInner = new StaticClassExample.ClsInner();

        objClsInner.dispMessage();
    }
}
import java.util.*;

public class StaticClassExample {
    static int a = 0;

    //static class declaration
    static class ClsInner {
        //static method of static class
        public static void dispMessage() {
            a = 20;
            System.out.println("Value of a: " + a);
        }
    }

    //main()
    public static void main(String[] s) {
        //no need to create object
        StaticClassExample.ClsInner.dispMessage();
    }
}

Output

Value of a: 20
Value of a: 20

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply