• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Can any one please explain this

 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since the names of local classes are not visible outside the local context, references of these classes cannot be declared outside. So their functionality could be accessed only via super-class references (either interfaces or classes). Objects of those class types are created inside methods and returned as super-class type references to the outside world. This is the reason that they can only access final variables within the local block. That way, the value of the variable can be always made available to the objects returned from the local context to outside world.
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is this a quote from somewhere?
 
Mahesh Bansal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This quote is from Velu's note on inner classes.
Mahesh
 
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Mahesh Bansal,
The Quote you have written is in context of Insatantiating Local Classes.
A Local class can be instantiated in the block in which it is defined. A method can return an instance of the local class. The local class type must then be assignable to the return type of the method.
It cannot be the same as the local class type, since this type is not accessible outside of the method. Often a superttype (superclass or interface) of the local class is specified as the return type.
consider the following example from Mughal Khalid.
interface IDrawable { // 1
void draw();
}
class Shape implements IDrawable { // 2
public void draw() {
System.out.println("Drawing a Sahpe.");
}
}
class Painter { // 3 Top-level class
public Shape createCircle(final double radius) { // 4 Non-static method
class Circle extends Shape { // 5 Non-static local class
public void draw() {
System.out.println("Drawing a Circle of radius: " + radius);
}
}
return new Circle(); // 6 Object of non-static local class
}
public static IDrawable createMap() { // 7 static method
class Map implements IDrawable { // 8 Static local class
public void draw() {
System.out.println("Drawing a Map.");
}
}
return new Map(); // 9 Object of static local class
}
}
public class Client {
public static void main(String args[]) {
IDrawable [] drawables = { // 10
new Painter().createCircle(5), // 11 Object of non-static local class
Painter.createMap(), // 12 Object of static class
new Painter().createMap() // 13 Object of static local class
};
for (int i=0; i < drawables.length; i++) // 14
drawables[i].draw();
System.out.println("Local Class Names: ");
System.out.println(drawables[0].getClass()); // 15
System.out.println(drawables[1].getClass()); // 16
}
}
Output from the program:
Drawing a Circle of radius: 5.0
Draeing a Map.
Drawing a Map.
Local Class Names:
Class Painter$1$Circle
Class Painter$1$Map
Creating an instance of a non-static local class requires an instance of the enclosing class. The non-static method createCircle() is invoked on the instance of the enclosing class to create an instance of the non-static local class, as shown at 11. In the non-static method, the reference to the instance of the enclosing context is passed implicitly in the constructor call of the non-static local class at 6.
A static method can be invoked either through the class name or through instance of the class. An instance of a static local class can be created in either way, by calling the createmap() method as shown at 12 and 13. As might be expected, no outer object is involved.
"As references to a local class cannot be declared outside of the local context, the functionality of the class is only available through supertype references."
Hope this help you out.
Regards,
Raj.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic