It is possible to define a class
within another class such classes are known as nested classes. A nested class has access to all of the
variables and methods of its outer class.
But the outer class doesn’t access the variables and methods of nested
class. Since the scope of a nested class
is bounded by the scope of its enclosing class.
There
are two types of nested classes: static and non-static
A static nested class is a class
started with ‘static’ modifier. Due to
the static it must access the members of its enclosing class through an
object. Direct access cannot be possible.
A non-static class is a class as ordinary
representation of class. The most
important type of the non-static class is “Inner class”. The inner class has access to all of the
variables and methods of its outer class.
Thus, inner class scope is fully within the scope of its outer class.
Example:
class outer
{
int
out_x=34;
void
test()
{
inner
in=new inner();
in.display();
}
class
inner
{
void
display()
{
System.out.println("value="+out_x);
}
}
}
class test
{
public
static void main(String[] args)
{
outer
y=new outer();
y.test();
}
}
No comments:
Post a Comment