File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Problem about inner class ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problem about inner class ?" Watch "Problem about inner class ?" New topic

Problem about inner class ?

Jack Lau
Ranch Hand

Joined: Aug 30, 2002
Posts: 168
In the following code segment 2, I do not know why class B is put inside method dest rather than outside the method (in code segment 1). Could anyone tell me what is the purpose of putting class inside method ? Is it better than put it outside the method ?
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Example 2 illustrates what is known as a local class. It can only be instantiated from within the dest() method. Since the method returns a reference to C, the calling method can use the object as if it is a C. However, it cannot cast it to a B since that class delcaration is unavailable.
As for which way is better, it depends on the design constraints. In fact, this is the answer for most similar questions in programming. Understanding the different ways you can declare a class will help you decide which fits a certain set of design criteria.

Java API Documentation
The Java Tutorial
Jack Lau
Ranch Hand

Joined: Aug 30, 2002
Posts: 168
Thank you !!
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: Problem about inner class ?
jQuery in Action, 3rd edition