InterviewSolution
Saved Bookmarks
| 1. |
Difference between inner class and nested class in Java? |
|
Answer» A nested class in Java is declared inside a class or interface. There are two types of nested CLASSES i.e. static and non-static. The non-static nested classes are known as inner class in Java whereas the static nested classes are merely known as nested class. A program that demonstrates an inner class in Java is GIVEN as follows: class Outer { class Inner { public void display() { System.out.println("Inside the inner class METHOD"); } } } public class Main { public static void main(String[] args) { Outer.Inner OBJ = new Outer().new Inner(); obj.display(); } }The output of the above program is as follows: Inside the inner class methodA program that demonstrates the nested class in Java is given as follows: class Outer { static int num = 67; static class Inner { public void display() { System.out.println("The number is: " + num); } } } public class Main { public static void main(String[] args) { Outer.Inner obj = new Outer.Inner(); obj.display(); } }The output of the above program is as follows: The number is: 67 |
|