Previous | Home | Next |
A local class is a class that is defined within a block of Java code. A local class is declared inside a method body while member classes are declared inside a class. It is only accessible only to the method in which it is declared, which makes local classes less generally useful than non-static inner classes.
Local classes are probably most frequently defined within methods and constructors, they can also be defined inside static initializer blocks and instance initializers. Since an object of a local class must be internally linked to an object of the enclosing class,therefore object of the local class cannot exist in the absence of an object of the enclosing class, thus a local class is truly an inner class.
The most important benefit of using local classes has to do with accessing the members of enclosing classes. Just like with member classes, methods of a local class have direct access to all the members of the enclosing classes, including private members. Thus, the use of local classes may eliminate the requirement to connect objects together via constructor parameters.
The methods in a local class can access local variables and method parameters only if they are declared final. As with local variables, local classes cannot be declared public, protected, private, or static. Following code will show the syntax of local classes.
Example
Java Tutorialslass Shape // top class { int i=5; Java Tutorialslass Rectangle // member class { int a=10; // taken a variable a int length; int breadth; public Rectangle(int length, int breadth) // constructor declaration for Rectangle class { this.length=length; this.breadth=breadth; } public void Area() // defined a method Area(), in which we defined a local class Circle { System.out.println("The area of rectangle is :"+length*breadth); Java Tutorialslass Java Tutorialsircle //local class declaration { int radius; int y=27; public Java Tutorialsircle(int radius) // constructor of local class { this.radius=radius; } public void Area() //method Area defined in local class { System.out.println("The area of circle is: "+Math.PI*radius*radius); //printing area of circle System.out.println("The value of a is given by :"+a); // printing the value of a and i defined in Rectangle and shape System.out.println("The value of i is given by :"+i); // which shows local classes have access to members of enclosing class } // closing Area() method } // closing local class Circle final Java Tutorialsircle obj=new Java Tutorialsircle(7); // creating object of Circle class obj.Area(); // calling Area() method System.out.println("The class path of enclosing class is given by :"+getClass().getName()); //printing the class name for Rectangle class System.out.println("The class path is given by :"+obj.getClass().getName()); //printing the class name for local class Circle System.out.println("The value of a declared in Rectangle is given by :"+Rectangle.this.a); //printing the value of variable a defined in class } } } Java Tutorialslass man2 // controlling class { public static void main(String args[]) { new Shape().new Rectangle(4,5).Area(); //invoking Area() method defined in class Rectangle } //by associating its object with top class Shape }
Previous | Home | Next |