Q:

Java program to explain static class

0

Java program to explain static class

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Static Class with Non Static Method:

//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();
    }
}

Output:

Value of a: 20

Static Class with Static Method:

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

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now