♠ Posted by Unknown in Core Java at 10:54
Java Inner Class Example
It is possible to define a class
within another class, such classes are known as nested classes. The scope of a
nested class is bounded by the scope of its enclosing class. Thus, if class B
is defined within class A, then B is known to A, but not outside of A. a nested
class has access to the members, including private members, of the class in
which it is nested. However, the enclosing class does not have access to
the members of the nested class.
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.
Thus, an inner class is fully within the scope of its enclosing class.
import java.io.*;
import java.lang.*;
class
OuterClass
{
private
int out;
public
void Display_Outer()
{
InnerClass
iObj = new InnerClass(10);
System.out.print("First
in Outer Class : ");
iObj.Display_Inner();
}
class
InnerClass
{
public
InnerClass(int x)
{
out = x; }
public
void Display_Inner()
{
System.out.println("Value
of Outer Class Variable : " + out);
}
};
};
public class InnerExample
{
public
static void main(String[] args)
{
OuterClass
obj = new OuterClass();
obj.Display_Outer();
}
};
0 comments:
Post a Comment