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.
public class Outer_Class
{
static Outer_Class.InnerClass obj;
void test1()
{
System.out.println("Success");
}
static public class InnerClass
{
private String name = "Peakit";
public void test2()
{
Outer_Class outer = new Outer_Class();
outer.test1();
}
}
public static void main(String[] args)
{
obj = new Outer_Class.InnerClass();
obj.test2();
}
}
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.
Next Steps:
- Get Free Certificate of Merit in Java Programming
- Participate in Java Programming Certification Contest
- Become a Top Ranker in Java Programming
- Take Java Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Related Posts:
- Practice Information Technology MCQs
- Buy Programming Books
- Practice BCA MCQs
- Apply for Information Technology Internship
- Buy Java Books