1.

Write a program in Java, to show nesting of classes.

Answer»

Nesting of classes means writing a class inside another class. The inner classes in Java are usually static. This happens when we don’t want to use the variable of the outer class in the inner class. This helps to create an instance/object of the inner class without creating an instance of the Outer class. Following is a program to show the nesting of classes in Java.

Java Code

public class Main {

public static void main(String[] args) {
Outer obj1 = new Outer(10, 20);

Outer.Inner2 obj2 = new Outer.Inner2(40);
obj2.showData();

Outer.Inner3.z = 100;
System.out.println(Outer.Inner3.z);
}

}

class Outer {

private int x, y;

Outer() {
System.out.println("Outer class default constructor called");
}

Outer(int x, int y) {
this.x = x;
this.y = y;
}

void showData() {
System.out.println("the value of x is:" + x + " and the value of y is: " + y);
}

class Inner1 {

int z = 0;

Inner1() {
System.out.println("Inner class default constructor called");
}

Inner1(int z) {
this.z = z;
}

void showData() {
System.out.println("The value of x is: " + x + " the value of y is: " + y + " and z is: " + z);
}
}

static class Inner2 {

int z = 0;

Inner2() {
System.out.println("Inner class default constructor called");
}

Inner2(int z) {
this.z = z;
}

void showData() {
System.out.println("The value of z is: " + z);
}
}

static class Inner3 {

static int z = 0;

Inner3() {
System.out.println("Inner class default constructor called");
}

Inner3(int a) {
z = a;
}

void showData() {
System.out.println("The value of z is: " + z);
}
}

Output

The value of z is: 40
100


Discussion

No Comment Found