wood burning stoves 2.0*
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problem about inner class ?" Watch "Problem about inner class ?" New topic
Author

Problem about inner class ?

Jack Lau
Ranch Hand

Joined: Aug 30, 2002
Posts: 166
Hello,
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 ?
Thx!
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: 166
Thank you !!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Problem about inner class ?
 
Similar Threads
Polymorphic Methods
Flight - Segment another view
a problem with the resolt of the code
Doubts in HttpSessionBindingListener
Mock question on polymorphism