Java Program to Illustrates Use of Referencing the Object from Inner Class

«
»
This is a Java Program to Illustrates Use of Referencing the Object from Inner Class.

It is possible to define a class within another class, such classes are known as nested classes. The most important type of nested class is the inner class. An inner class is a non-static nested class. It has access to all of the variables and methods of its outer class and may refer to them directly in the same way that other non-static members of the outer class do.

Here is the source code of the Java Program to Illustrates Use of Referencing the Object from Inner Class. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. public class Outer_Class
  2. {
  3.     static Outer_Class.InnerClass obj;
  4.     void test1()
  5.     {
  6.         System.out.println("Success");
  7.     }
  8.     static public class InnerClass
  9.     {
  10.     	private String name = "Peakit";
  11.         public void test2()
  12.         {
  13.             Outer_Class outer = new Outer_Class();
  14.             outer.test1();
  15.         }
  16.     }
  17.     public static void main(String[] args)
  18.     {
  19.         obj = new Outer_Class.InnerClass();
  20.         obj.test2();
  21.     }
  22. }

Output:

$ javac Outer_Class.java
$ java  Outer_Class
 
Success

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.