This is a Java Program to Illustrates Use of Instance Inner Class. An inner class has access to all of the members of its enclosing class, but the reverse is not true.
Here we access the members of the outer class named outer in the inner class named inner. But the members of inner class cannot be used by the outer class.
Here is the source code of the Java Program to Illustrates Use of Instance Inner Class. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.
class Outer
{
int outer_x = 100;
void test()
{
Inner inner = new Inner();
inner.display();
}
class Inner
{
int y = 10;
void display()
{
System.out.println("display: outer_x = " + outer_x);
}
}
void showy()
{
System.out.println(y); // error, y not known here!
}
}
class InnerDemo
{
public static void main(String args[])
{
Outer outer = new Outer();
outer.test();
}
}
Output:
$ javac InnerDemo.java $ java InnerDemo display: outer_x = 100
Sanfoundry Global Education & Learning Series – 1000 Java Programs.
advertisement
advertisement
Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.
If you find any mistake above, kindly email to [email protected]Related Posts:
- Check Java Books
- Practice Programming MCQs
- Practice BCA MCQs
- Check Programming Books
- Practice Information Technology MCQs