aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Can any one please explain this Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Can any one please explain this " Watch "Can any one please explain this " New topic
Author

Can any one please explain this

Mahesh Bansal
Ranch Hand

Joined: Nov 28, 2000
Posts: 82
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.
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Is this a quote from somewhere?


"JavaRanch, where the deer and the Certified play" - David O'Meara
Mahesh Bansal
Ranch Hand

Joined: Nov 28, 2000
Posts: 82
This quote is from Velu's note on inner classes.
Mahesh
Rajpal Kandhari
Ranch Hand

Joined: Aug 26, 2000
Posts: 126
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.

Regards,<P>Raj.<BR>-------------------------<BR>Afforts should be Appriciated.<BR>-------------------------
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can any one please explain this