A class is a non-primitive or user-defined data type in Java, while an object is an instance of a class. Local classes are classes that are defined in a block, which is a group of zero or more statements between balanced braces. You typically find local classes defined in the body of a method.
This section covers the following topics: Declaring Local Classes. Accessing Members of an Enclosing Class. A class i.e. created inside a method is called local inner class in java. If you want to invoke the methods of local inner class, you must instantiate this class inside the method.
Syntax:
class Class_Name // OuterClass
{
void method ( )
{
class Class_Name // NestedClass
{
. . .
}
}
}
The Outercls class contains a method display() which defines a local inner class called Inner. The Inner class has a method innerDisplay() which prints "Inner Display" to the console.
Within the display() method of Outercls, an object of Inner class is created and its innerDisplay() method is called to print "Inner Display" to the console.
The main() method in localInnerClass class creates an object of Outercls class and calls its display() method to execute the code inside it and print "Inner Display" to the console.
//Local Inner Class class Outercls { void display() { class Inner { void innerDisplay() { System.out.println("Inner Display"); } } Inner i =new Inner(); i.innerDisplay(); } } public class localInnerClass { public static void main(String[] args) { Outercls o =new Outercls(); o.display(); } }
Inner DisplayTo download raw file Click Here
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions