Skip to main content

Write a program that demonstrate inner class and static fields.

This program defines an outer class called OuterClass that contains a static field called staticField and an instance field called instanceField. It also defines two inner classes: InnerStaticClass, which has a method that prints the value of the static field, and InnerInstanceClass, which has a method that prints the value of the instance field.

The main method creates an instance of the outer class, outer, and then creates instances of both inner classes, innerInstance and innerStatic. It then uses these instances to access the instance and static fields, respectively.

Note that the InnerInstanceClass requires an instance of the outer class in order to be created, whereas the InnerStaticClass can be created without an instance of the outer class. This demonstrates the difference between inner instance classes and inner static classes.

public class OuterClass {
    private static int staticField = 10;
    private int instanceField = 20;

    public static class InnerStaticClass {
        public void printStaticField() {
            System.out.println("Static field: " + staticField);
        }
    }

    public class InnerInstanceClass {
        public void printInstanceField() {
            System.out.println("Instance field: " + instanceField);
        }
    }

    public static void main(String[] args) {
        // Create an instance of the outer class
        OuterClass outer = new OuterClass();

        // Create an instance of the inner instance class
        OuterClass.InnerInstanceClass innerInstance = outer.new InnerInstanceClass();

        // Create an instance of the inner static class
        OuterClass.InnerStaticClass innerStatic = new OuterClass.InnerStaticClass();

        // Access the instance field from the inner instance class
        innerInstance.printInstanceField();

        // Access the static field from the inner static class
        innerStatic.printStaticField();
    }
}

When you run this program, it will output:

Instance field: 20
Static field: 10

Comments

Popular posts from this blog

Find mean, mode and median of a vector in python

Find mean, mode and median of a vector in python To find the mean, mode, and median of a vector in Python, you can use the following implementation: def find_max_index (arr): if len(arr) == 1 : return 0 else : max_index = find_max_index(arr[ 1 :]) return (max_indexdef factorial(n): if n == 0 : return 1 else : return n * factorial(n - 1 ) n = int(input( "Enter a Number : " )) fact = factorial(n) print (fact) + 1 ) if arr[ 0 ] < arr[max_index + 1 ] else 0 arr = [ 4 , 6 , 2 , 8 , 5 ] max_index = find_max_index(arr) print (max_index) # Output: 3 This code first imports the statistics module, which provides functions for calculating statistical properties of a dataset. It then defines a vector with some sample data. You can replace this with your own vector. To calculate the mean of the vector, it first calculates the sum of all the elements in the vector using...

Find greatest common divisor using tail recursion in python

Find  greatest common divisor using tail recursion in python To find the greatest common divisor (GCD) of two numbers using tail recursion in Python, you can use the Euclidean algorithm. Here's an example implementation: def gcd (a, b): if b == 0 : return a else : return gcd(b, a % b) a = int(input( "Enter a First Number : " )) b = int(input( "Enter a Second Number : " )) print (gcd(a, b)) This function takes two arguments, a and b, and returns their GCD. It checks if b is zero; if it is, it returns a as the GCD. Otherwise, it makes a recursive call to gcd with b and a % b as the arguments. The recursive call is a tail call, which means that it's the last operation performed in the function. This allows the Python interpreter to optimize the function's execution by reusing the same stack frame for the recursive call, instead of creating a new one.

Write a program that demonstrate string operations using StringBuffer class.

This program demonstrates several operations that can be performed using the StringBuffer class. It creates a new StringBuffer object with the initial value "Hello, ", and then appends "world!" to the end of the string using the append() method. It then inserts "Java " into the middle of the string using the insert() method, replaces "Hello" with "Hi" using the replace() method, deletes the first three characters of the string using the delete() method, and reverses the order of the characters in the string using the reverse() method. public class StringBufferDemo { public static void main ( String [] args ) { // Create a new StringBuffer object StringBuffer sb = new StringBuffer ( "Hello, " ); // Append a string to the StringBuffer sb . append ( "world!" ); System . out . println ( sb ); // Insert a string into the middle of the StringBuffer ...