TechieDrill Your World Of Technical Tutorials

Java Inner class

12.06.2009 · Posted in Java
class Outer
{
String s=”Welcome”;
public void MethodInner()
{
Inner in=new Inner();
in.InnerMethod();
}
class Inner
{
public void InnerMethod()
{
System.out.println(”Hello!”+s);
}
}
}
class RegInnerClass
{
public static void main(String args[])
{
Outer ou=new Outer();
ou.MethodInner();
}
}

Classes that are defined inside another class are called inner class or nested class. Inner classes benefits in access control and naming control.

Try out the below code

class Outer

{

String s=”Welcome”;

public void MethodInner()

{

Inner in=new Inner();

in.InnerMethod();

}

class Inner

{

public void InnerMethod()

{

System.out.println(”Hello!”+s);

}

}

}

class RegInnerClass

{

public static void main(String args[])

{

Outer ou=new Outer();

ou.MethodInner();

}

}

Tags: , ,

Leave a Reply

You must be logged in to post a comment.